Skip to content

Commit

Permalink
Make link paste work
Browse files Browse the repository at this point in the history
  • Loading branch information
raimohanska committed Mar 17, 2024
1 parent 816b83c commit 86f9e04
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
9 changes: 5 additions & 4 deletions frontend/src/board/CollaborativeTextView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import { QuillBinding } from "y-quill"
import {
AccessLevel,
Board,
TextItem,
canWrite,
getAlign,
getHorizontalAlign,
getItemBackground,
TextItem,
} from "../../../common/src/domain"
import { Dispatch } from "../store/board-store"
import { CRDTStore } from "../store/crdt-store"
import { getAlignItems } from "./ItemView"
import { BoardFocus } from "./board-focus"
import { contrastingColor } from "./contrasting-color"
import { getAlignItems } from "./ItemView"
import { ToolController } from "./tool-selection"
import { preventDoubleClick } from "./double-click"
import PasteLinkOverText from "./quillPasteLinkOverText"

Quill.register("modules/cursors", QuillCursors)
Quill.register("modules/pasteLinkOverText", PasteLinkOverText)

interface CollaborativeTextViewProps {
item: L.Property<TextItem>
Expand Down Expand Up @@ -56,6 +56,7 @@ export function CollaborativeTextView({
modules: {
cursors: true,
toolbar: false,
pasteLinkOverText: true,
history: {
userOnly: true, // Local undo shouldn't undo changes from remote users
},
Expand Down
44 changes: 44 additions & 0 deletions frontend/src/board/quillPasteLinkOverText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Quill from "quill"
import { isURL } from "../components/sanitizeHTML"

declare global {
interface Window {
Quill?: typeof Quill
}
}

export default class PasteLinkOverText {
quill: Quill

constructor(quill: Quill) {
this.quill = quill
this.registerPasteListener()
}
registerPasteListener() {
this.quill.clipboard.addMatcher(Node.TEXT_NODE, (node, delta) => {
if (typeof node.data !== "string") {
return undefined as any // This is how it was written
}

const url = node.data
if (isURL(url)) {
const sel = (this.quill as any).selection.savedRange as { index: number; length: number } | null
if (sel && sel.length > 0) {
const existing = this.quill.getContents(sel.index, sel.length)
if (existing.ops.length === 1 && typeof existing.ops[0].insert === "string") {
const existingText = existing.ops[0].insert
delta.ops = [{ insert: existingText, attributes: { link: url } }]
}
} else {
delta.ops = [{ insert: url, attributes: { link: url } }]
}
}

return delta
})
}
}

if (window != null && window.Quill) {
window.Quill.register("modules/pasteLinkOverText", PasteLinkOverText)
}

0 comments on commit 86f9e04

Please sign in to comment.