@@ -6,7 +6,8 @@ import { type EditorView } from "@codemirror/view"
|
||||
import { MAXIMUM_FILE_SIZE } from "~/lib/constants"
|
||||
import { UploadFile } from "~/lib/upload-file"
|
||||
|
||||
import { ICommand, wrapExecute } from "."
|
||||
import type { ICommand } from "."
|
||||
import { wrapExecute } from "./helper"
|
||||
|
||||
export async function editorUpload(file: File, view: EditorView) {
|
||||
const toastId = toast.loading("Uploading...")
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { EditorSelection } from "@codemirror/state"
|
||||
import { EditorView } from "@codemirror/view"
|
||||
|
||||
export type IWrapExecute = {
|
||||
view: EditorView
|
||||
prepend: string
|
||||
append: string
|
||||
}
|
||||
export const wrapExecute = ({ view, prepend, append }: IWrapExecute) => {
|
||||
const range = view.state.selection.ranges[0]
|
||||
const selection = view.state.sliceDoc(
|
||||
range.from - prepend.length,
|
||||
range.to + append.length,
|
||||
)
|
||||
if (selection.startsWith(prepend) && selection.endsWith(append)) {
|
||||
view.dispatch(
|
||||
view.state.changeByRange((range) => ({
|
||||
changes: [
|
||||
{
|
||||
from: range.from - prepend.length,
|
||||
to: range.to + append.length,
|
||||
insert: view.state.sliceDoc(range.from, range.to),
|
||||
},
|
||||
],
|
||||
range: EditorSelection.range(
|
||||
range.from - prepend.length,
|
||||
range.to - prepend.length,
|
||||
),
|
||||
})),
|
||||
)
|
||||
view.focus()
|
||||
return
|
||||
}
|
||||
view.dispatch(
|
||||
view.state.changeByRange((range) => ({
|
||||
changes: [
|
||||
{ from: range.from, insert: prepend },
|
||||
{ from: range.to, insert: append },
|
||||
],
|
||||
range: EditorSelection.range(
|
||||
range.from + prepend.length,
|
||||
range.to + prepend.length,
|
||||
),
|
||||
})),
|
||||
)
|
||||
view.focus()
|
||||
}
|
||||
+3
-47
@@ -49,12 +49,6 @@ export type IPrependExecute = {
|
||||
prepend: string
|
||||
}
|
||||
|
||||
export type IWrapExecute = {
|
||||
view: EditorView
|
||||
prepend: string
|
||||
append: string
|
||||
}
|
||||
|
||||
export const prependExecute = ({ view, prepend }: IPrependExecute) => {
|
||||
const range = view.state.selection.ranges[0]
|
||||
const selection = view.state.sliceDoc(range.from - prepend.length, range.to)
|
||||
@@ -90,47 +84,6 @@ export const prependExecute = ({ view, prepend }: IPrependExecute) => {
|
||||
)
|
||||
view.focus()
|
||||
}
|
||||
|
||||
export const wrapExecute = ({ view, prepend, append }: IWrapExecute) => {
|
||||
const range = view.state.selection.ranges[0]
|
||||
const selection = view.state.sliceDoc(
|
||||
range.from - prepend.length,
|
||||
range.to + append.length,
|
||||
)
|
||||
if (selection.startsWith(prepend) && selection.endsWith(append)) {
|
||||
view.dispatch(
|
||||
view.state.changeByRange((range) => ({
|
||||
changes: [
|
||||
{
|
||||
from: range.from - prepend.length,
|
||||
to: range.to + append.length,
|
||||
insert: view.state.sliceDoc(range.from, range.to),
|
||||
},
|
||||
],
|
||||
range: EditorSelection.range(
|
||||
range.from - prepend.length,
|
||||
range.to - prepend.length,
|
||||
),
|
||||
})),
|
||||
)
|
||||
view.focus()
|
||||
return
|
||||
}
|
||||
view.dispatch(
|
||||
view.state.changeByRange((range) => ({
|
||||
changes: [
|
||||
{ from: range.from, insert: prepend },
|
||||
{ from: range.to, insert: append },
|
||||
],
|
||||
range: EditorSelection.range(
|
||||
range.from + prepend.length,
|
||||
range.to + prepend.length,
|
||||
),
|
||||
})),
|
||||
)
|
||||
view.focus()
|
||||
}
|
||||
|
||||
export const toolbars: ICommand[] = [
|
||||
Heading,
|
||||
Bold,
|
||||
@@ -152,3 +105,6 @@ export const toolbars: ICommand[] = [
|
||||
Cloud,
|
||||
Help,
|
||||
]
|
||||
|
||||
export { wrapExecute } from "./helper"
|
||||
export type { IWrapExecute } from "./helper"
|
||||
|
||||
Reference in New Issue
Block a user