Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: paste border number into border number #2271

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions frontend/src/components/input/TextEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { CustomBulletList } from "@/editor/bulletList"
import addBorderNumbers from "@/editor/commands/addBorderNumbers"
import { handleSelection } from "@/editor/commands/handleSelection"
import removeBorderNumbers from "@/editor/commands/removeBorderNumbers"
import { EventHandler } from "@/editor/EventHandler"
import { FontSize } from "@/editor/fontSize"
import { CustomImage } from "@/editor/image"
import { Indent } from "@/editor/indent"
Expand Down Expand Up @@ -110,6 +111,7 @@ const editor = new Editor({
BorderNumberLink,
Bold,
Color,
EventHandler,
FontSize,
Italic,
CustomListItem,
Expand Down
106 changes: 106 additions & 0 deletions frontend/src/editor/EventHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Extension } from "@tiptap/core"
import { Plugin, PluginKey } from "@tiptap/pm/state"

export const EventHandler = Extension.create({
name: "eventHandler",

addProseMirrorPlugins() {
return [
new Plugin({
key: new PluginKey("eventHandler"),
props: {
handlePaste: (view, event) => {
const { state } = view
const { selection } = state
const { from } = selection
const $from = state.doc.resolve(from)

const parent = $from.node($from.depth - 1)
const schema = state.schema
const borderNumberContentNodeType = schema.nodes.borderNumberContent

if (parent.type !== borderNumberContentNodeType) {
return false
}

const clipboardData = event.clipboardData
const pastedHTML = clipboardData?.getData("text/html")

if (pastedHTML) {
const parser = new DOMParser()
const doc = parser.parseFromString(pastedHTML, "text/html")

const borderNumberElements = doc.querySelectorAll("border-number")
if (borderNumberElements.length > 0) {
const borderNumberContentElements =
doc.querySelectorAll("content")

if (borderNumberContentElements.length > 0) {
Array.from(borderNumberContentElements)
.reverse()
.forEach((contentNode) => {
this.editor.commands.insertContentAt(
from,
contentNode.outerHTML,
)
})

return true
}

return false
}
return false
}
return false
},
handleDrop: (view, event) => {
const { state } = view
const { selection } = state
const { from } = selection
const $from = state.doc.resolve(from)

const parent = $from.node($from.depth - 1)
const schema = state.schema
const borderNumberContentNodeType = schema.nodes.borderNumberContent

if (parent.type !== borderNumberContentNodeType) {
return false
}

const dataTransfer = event.dataTransfer
const draggedHTML = dataTransfer?.getData("text/html")

if (draggedHTML) {
const parser = new DOMParser()
const doc = parser.parseFromString(draggedHTML, "text/html")

const borderNumberElements = doc.querySelectorAll("border-number")
if (borderNumberElements.length > 0) {
const borderNumberContentElements =
doc.querySelectorAll("content")

if (borderNumberContentElements.length > 0) {
Array.from(borderNumberContentElements)
.reverse()
.forEach((contentNode) => {
this.editor.commands.insertContentAt(
from,
contentNode.outerHTML,
)
})

return true
}

return false
}
return false
}
return false
},
},
}),
]
},
})