@krainovsd/markdown-editor 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/{index-D7RdBXAA.js → index-BgxbH3r2.js} +68 -41
- package/lib/cjs/index-BgxbH3r2.js.map +1 -0
- package/lib/cjs/{index-nYYQ5dvl.js → index-DU8JMAfc.js} +2 -2
- package/lib/cjs/{index-nYYQ5dvl.js.map → index-DU8JMAfc.js.map} +1 -1
- package/lib/cjs/index.js +1 -1
- package/lib/esm/extensions/markdown/image/image-decoration.js +7 -4
- package/lib/esm/extensions/markdown/image/image-decoration.js.map +1 -1
- package/lib/esm/extensions/markdown/image/image-widget.js +24 -10
- package/lib/esm/extensions/markdown/image/image-widget.js.map +1 -1
- package/lib/esm/extensions/markdown/italic/italic-decoration.js +6 -4
- package/lib/esm/extensions/markdown/italic/italic-decoration.js.map +1 -1
- package/lib/esm/extensions/markdown/link/auto-link-decoration.js +3 -2
- package/lib/esm/extensions/markdown/link/auto-link-decoration.js.map +1 -1
- package/lib/esm/extensions/markdown/link/link-decoration.js +5 -4
- package/lib/esm/extensions/markdown/link/link-decoration.js.map +1 -1
- package/lib/esm/extensions/markdown/link/link-widget.js +4 -2
- package/lib/esm/extensions/markdown/link/link-widget.js.map +1 -1
- package/lib/esm/extensions/markdown/markdown-decoration.js +5 -10
- package/lib/esm/extensions/markdown/markdown-decoration.js.map +1 -1
- package/lib/esm/extensions/markdown/markdown-state.js +4 -5
- package/lib/esm/extensions/markdown/markdown-state.js.map +1 -1
- package/lib/esm/lib/utils/random-string.js +13 -0
- package/lib/esm/lib/utils/random-string.js.map +1 -0
- package/lib/index.d.ts +5 -0
- package/package.json +1 -1
- package/lib/cjs/index-D7RdBXAA.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image-widget.js","sources":["../../../../../src/extensions/markdown/image/image-widget.ts"],"sourcesContent":["import { type EditorView, WidgetType } from \"@codemirror/view\";\nimport { saveDispatch } from \"@/lib/utils\";\nimport { openedImageEffect } from \"../markdown-state\";\nimport styles from \"../styles.module.scss\";\n\nconst INTERVAL_DELAY = 10000;\nconst IMAGE_NODES: Record<string, ImageElement | undefined> = {};\nconst EXISTING_WIDGETS: Set<string> = new Set();\nlet interval: NodeJS.Timeout | null = null;\n\ninterface ImageElement extends HTMLImageElement {\n clearListeners?: () => void;\n destroy?: () => void;\n}\n\nexport class ImageWidget extends WidgetType {\n constructor(\n private text: string,\n private link: string,\n private from: number,\n private to: number,\n private imageSrcGetter: ((src: string) => string) | undefined,\n private view: EditorView,\n ) {\n super();\n }\n\n get key() {\n return `${this.link}:${this.text}:${this.from}:${this.to}`;\n }\n\n get src() {\n return this.imageSrcGetter ? this.imageSrcGetter(this.link) : this.link;\n }\n\n eq(widget: ImageWidget): boolean {\n const image = IMAGE_NODES[this.key];\n\n if (!image) return false;\n\n delete IMAGE_NODES[this.key];\n EXISTING_WIDGETS.delete(this.key);\n\n if (image.src !== widget.src) image.src = widget.src;\n if (image.alt !== widget.text) image.alt = widget.text;\n\n this.link = widget.link;\n this.text = widget.text;\n this.from = widget.from;\n this.to = widget.to;\n\n this.registerListeners(image);\n IMAGE_NODES[this.key] = image;\n EXISTING_WIDGETS.add(this.key);\n\n return true;\n }\n\n updateDOM(): boolean {\n return true;\n }\n\n toDOM(): HTMLElement {\n EXISTING_WIDGETS.add(this.key);\n\n let image = IMAGE_NODES[this.key];\n if (image) {\n if (image.src !== this.src) {\n image.src = this.src;\n }\n if (image.alt !== this.text) image.alt = this.text;\n\n return image;\n }\n\n image = document.createElement(\"img\");\n image.classList.add(styles.image);\n image.alt = this.text;\n image.src = this.src;\n image.style.maxWidth = \"100%\";\n\n this.registerListeners(image);\n IMAGE_NODES[this.key] = image;\n\n if (!interval) interval = setInterval(garbageCollectorInterval, INTERVAL_DELAY);\n\n return image;\n }\n\n destroy(): void {\n EXISTING_WIDGETS.delete(this.key);\n }\n\n registerListeners(image: ImageElement) {\n image.clearListeners?.();\n const abortController = new AbortController();\n image.addEventListener(\n \"mousedown\",\n (event) => handleClick(this.view, this.text, this.link, this.key, event),\n { signal: abortController.signal },\n );\n image.clearListeners = () => {\n abortController.abort();\n };\n image.destroy = () => {\n image.clearListeners?.();\n image.remove();\n };\n }\n}\n\nfunction garbageCollectorInterval() {\n for (const [key, node] of Object.entries(IMAGE_NODES)) {\n if (EXISTING_WIDGETS.has(key) || !node) continue;\n\n delete IMAGE_NODES[key];\n node.destroy?.();\n }\n\n if (Object.keys(IMAGE_NODES).length === 0 && interval) {\n clearInterval(interval);\n interval = null;\n }\n}\n\n/** recursively find the link text node in line */\nfunction getTextNode(\n text: string,\n link: string,\n key: string,\n line: ChildNode | Node | null | undefined,\n): ChildNode | null {\n if (!line) return null;\n const textNodeContainer = getTextNodeContainer(text, link, key, line);\n if (!textNodeContainer) return null;\n\n for (const node of Array.from(textNodeContainer.childNodes)) {\n if (isCorrectNode(text, link, node)) {\n return node;\n }\n }\n\n return null;\n}\n\nfunction getTextNodeContainer(\n text: string,\n link: string,\n key: string,\n line: ChildNode | Node | null | undefined,\n): HTMLElement | null {\n if (!line) return null;\n\n for (const node of Array.from(line.childNodes)) {\n if (node instanceof HTMLElement && node.getAttribute(\"data-id\") === key) {\n return node;\n }\n\n if (node.nodeType !== 3) {\n const inner = getTextNodeContainer(text, link, key, node);\n if (inner) return inner;\n }\n }\n\n return null;\n}\n\nfunction isCorrectNode(\n text: string,\n link: string,\n node: ChildNode | Node | null | undefined,\n): node is ChildNode | Node {\n if (!node) return false;\n\n const textContent = node?.textContent;\n\n return Boolean(\n node &&\n textContent &&\n node.nodeType === 3 &&\n textContent.includes(link) &&\n textContent.includes(text),\n );\n}\n\ntype SelectLinkOptions = {\n node: ChildNode | Node;\n selection: Selection;\n start?: number;\n link: string;\n};\nfunction selectLink({ link, node, selection, start }: SelectLinkOptions) {\n const startPosition = start ?? (node.textContent?.indexOf?.(link) || 0);\n const endPosition = startPosition + link.length;\n\n const range = document.createRange();\n range.setStart(node, startPosition);\n range.setEnd(node, endPosition);\n selection.removeAllRanges();\n selection.addRange(range);\n}\n\nfunction handleClick(\n view: EditorView | undefined,\n text: string,\n link: string,\n key: string,\n event: MouseEvent,\n) {\n const selection = window.getSelection();\n\n if (event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) {\n return;\n }\n\n event.stopPropagation();\n event.preventDefault();\n const target = event.target as HTMLImageElement;\n const parent = target.parentNode;\n let line: HTMLElement | null = parent as HTMLElement | null;\n\n /** recursively find line that contains link */\n while (line && !line.classList.contains(\"cm-line\")) {\n line = line.parentNode as HTMLElement | null;\n }\n\n const editor = Array.from(document.querySelectorAll(\".cm-editor\")).find((element) =>\n element.contains(target),\n );\n\n if (!selection || !editor || !parent) return;\n\n const textNode = getTextNode(text, link, key, line);\n\n if (textNode) {\n return void selectLink({ selection, link, node: textNode });\n }\n\n saveDispatch(() => {\n if (!view) return;\n view.dispatch(view.state.update({ effects: openedImageEffect.of(key) }));\n\n const textNode = getTextNode(text, link, key, line);\n if (textNode) {\n selectLink({ selection, link, node: textNode });\n }\n\n requestAnimationFrame(() => {\n saveDispatch(() => {\n if (view) view.dispatch(view.state.update({ effects: openedImageEffect.of(undefined) }));\n });\n });\n });\n\n return false;\n}\n"],"names":[],"mappings":";;;;;AAKA,MAAM,cAAc,GAAG,KAAK;AAC5B,MAAM,WAAW,GAA6C,EAAE;AAChE,MAAM,gBAAgB,GAAgB,IAAI,GAAG,EAAE;AAC/C,IAAI,QAAQ,GAA0B,IAAI;AAOpC,MAAO,WAAY,SAAQ,UAAU,CAAA;AAE/B,IAAA,IAAA;AACA,IAAA,IAAA;AACA,IAAA,IAAA;AACA,IAAA,EAAA;AACA,IAAA,cAAA;AACA,IAAA,IAAA;IANV,WACU,CAAA,IAAY,EACZ,IAAY,EACZ,IAAY,EACZ,EAAU,EACV,cAAqD,EACrD,IAAgB,EAAA;AAExB,QAAA,KAAK,EAAE;QAPC,IAAI,CAAA,IAAA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,gBAAgB,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,KAC9E,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CACzB;AAED,IAAA,IAAI,CAAC,SAAS,IAAI,CAAC,MAAM,IAAI,CAAC,MAAM;QAAE;AAEtC,IAAA,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC;IAEnD,IAAI,QAAQ,EAAE;AACZ,QAAA,OAAO,KAAK,UAAU,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;;IAG7D,YAAY,CAAC,MAAK;AAChB,QAAA,IAAI,CAAC,IAAI;YAAE;QACX,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,iBAAiB,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;AAExE,QAAA,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC;QACnD,IAAI,QAAQ,EAAE;YACZ,UAAU,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;;QAGjD,qBAAqB,CAAC,MAAK;YACzB,YAAY,CAAC,MAAK;AAChB,gBAAA,IAAI,IAAI;oBAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,iBAAiB,CAAC,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;AAC1F,aAAC,CAAC;AACJ,SAAC,CAAC;AACJ,KAAC,CAAC;AAEF,IAAA,OAAO,KAAK;AACd;;;;"}
|
|
1
|
+
{"version":3,"file":"image-widget.js","sources":["../../../../../src/extensions/markdown/image/image-widget.ts"],"sourcesContent":["import { type EditorView, WidgetType } from \"@codemirror/view\";\nimport { saveDispatch } from \"@/lib/utils\";\nimport { openedImageEffect } from \"../markdown-state\";\nimport styles from \"../styles.module.scss\";\n\nconst IMAGE_NODES: Record<string, ImageContainerElement | undefined> = {};\nconst INTERVAL_DELAY = 10000;\nconst EXISTING_WIDGETS: Set<string> = new Set();\nlet interval: NodeJS.Timeout | null = null;\ninterface ImageContainerElement extends HTMLSpanElement {\n clearListeners?: () => void;\n destroy?: () => void;\n image?: HTMLImageElement;\n}\n\nexport class ImageWidget extends WidgetType {\n constructor(\n private text: string,\n private link: string,\n private from: number,\n private to: number,\n private uniqueId: string,\n private fullLine: boolean,\n private imageSrcGetter: ((src: string) => string) | undefined,\n private view: EditorView,\n ) {\n super();\n }\n\n get key() {\n return `${this.link}:${this.text}:${this.uniqueId}:${this.from}:${this.to}`;\n }\n\n get src() {\n return this.imageSrcGetter ? this.imageSrcGetter(this.link) : this.link;\n }\n\n eq(widget: ImageWidget): boolean {\n const container = IMAGE_NODES[this.key];\n const image = container?.image;\n\n if (!image) return false;\n\n delete IMAGE_NODES[this.key];\n EXISTING_WIDGETS.delete(this.key);\n\n if (image.src !== widget.src) image.src = widget.src;\n if (image.alt !== widget.text) image.alt = widget.text;\n\n this.link = widget.link;\n this.text = widget.text;\n this.from = widget.from;\n this.to = widget.to;\n\n this.registerListeners(image);\n IMAGE_NODES[this.key] = image;\n EXISTING_WIDGETS.add(this.key);\n\n return true;\n }\n\n updateDOM(): boolean {\n return true;\n }\n\n toDOM(): HTMLElement {\n EXISTING_WIDGETS.add(this.key);\n let container = IMAGE_NODES[this.key];\n let image = container?.image;\n\n if (image && container) {\n if (image.src !== this.src) {\n image.src = this.src;\n }\n if (image.alt !== this.text) image.alt = this.text;\n\n return container;\n }\n\n image = document.createElement(\"img\");\n image.classList.add(styles.image);\n image.alt = this.text;\n image.src = this.src;\n image.style.maxWidth = \"100%\";\n\n container = document.createElement(\"span\") as ImageContainerElement;\n container.appendChild(image);\n container.image = image;\n if (this.fullLine) {\n container.style.width = \"100%\";\n container.style.display = \"inline-block\";\n }\n\n this.registerListeners(container);\n IMAGE_NODES[this.key] = container;\n\n if (!interval) interval = setInterval(garbageCollectorInterval, INTERVAL_DELAY);\n\n return container;\n }\n\n destroy(): void {\n EXISTING_WIDGETS.delete(this.key);\n }\n\n registerListeners(image: ImageContainerElement) {\n image.clearListeners?.();\n const abortController = new AbortController();\n image.addEventListener(\n \"mousedown\",\n (event) => handleClick(this.view, this.text, this.link, this.key, event),\n { signal: abortController.signal },\n );\n image.clearListeners = () => {\n abortController.abort();\n };\n image.destroy = () => {\n image.clearListeners?.();\n image.remove();\n };\n }\n}\n\n/** for disable cache */\nfunction garbageCollectorInterval() {\n for (const [key, node] of Object.entries(IMAGE_NODES)) {\n if (EXISTING_WIDGETS.has(key) || !node) continue;\n delete IMAGE_NODES[key];\n node.destroy?.();\n }\n\n if (Object.keys(IMAGE_NODES).length === 0 && interval) {\n clearInterval(interval);\n interval = null;\n }\n}\n\n/** recursively find the link text node in line */\nfunction getTextNode(\n text: string,\n link: string,\n key: string,\n line: ChildNode | Node | null | undefined,\n): ChildNode | null {\n if (!line) return null;\n const textNodeContainer = getTextNodeContainer(text, link, key, line);\n if (!textNodeContainer) return null;\n\n for (const node of Array.from(textNodeContainer.childNodes)) {\n if (isCorrectNode(text, link, node)) {\n return node;\n }\n }\n\n return null;\n}\n\nfunction getTextNodeContainer(\n text: string,\n link: string,\n key: string,\n line: ChildNode | Node | null | undefined,\n): HTMLElement | null {\n if (!line) return null;\n\n for (const node of Array.from(line.childNodes)) {\n if (node instanceof HTMLElement && node.getAttribute(\"data-id\") === key) {\n return node;\n }\n\n if (node.nodeType !== 3) {\n const inner = getTextNodeContainer(text, link, key, node);\n if (inner) return inner;\n }\n }\n\n return null;\n}\n\nfunction isCorrectNode(\n text: string,\n link: string,\n node: ChildNode | Node | null | undefined,\n): node is ChildNode | Node {\n if (!node) return false;\n\n const textContent = node?.textContent;\n\n return Boolean(\n node &&\n textContent &&\n node.nodeType === 3 &&\n textContent.includes(link) &&\n textContent.includes(text),\n );\n}\n\ntype SelectLinkOptions = {\n node: ChildNode | Node;\n selection: Selection;\n start?: number;\n link: string;\n};\nfunction selectLink({ link, node, selection, start }: SelectLinkOptions) {\n const startPosition = start ?? (node.textContent?.indexOf?.(link) || 0);\n const endPosition = startPosition + link.length;\n\n const range = document.createRange();\n range.setStart(node, startPosition);\n range.setEnd(node, endPosition);\n selection.removeAllRanges();\n selection.addRange(range);\n}\n\nfunction handleClick(\n view: EditorView | undefined,\n text: string,\n link: string,\n key: string,\n event: MouseEvent,\n) {\n const selection = window.getSelection();\n\n if (event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) {\n return;\n }\n\n event.stopPropagation();\n event.preventDefault();\n const target = event.target as HTMLImageElement;\n const parent = target.parentNode;\n let line: HTMLElement | null = parent as HTMLElement | null;\n\n /** recursively find line that contains link */\n while (line && !line.classList.contains(\"cm-line\")) {\n line = line.parentNode as HTMLElement | null;\n }\n\n const editor = Array.from(document.querySelectorAll(\".cm-editor\")).find((element) =>\n element.contains(target),\n );\n\n if (!selection || !editor || !parent) return;\n\n const textNode = getTextNode(text, link, key, line);\n\n if (textNode) {\n return void selectLink({ selection, link, node: textNode });\n }\n\n saveDispatch(() => {\n if (!view) return;\n view.dispatch(view.state.update({ effects: openedImageEffect.of(key) }));\n\n const textNode = getTextNode(text, link, key, line);\n if (textNode) {\n selectLink({ selection, link, node: textNode });\n }\n\n requestAnimationFrame(() => {\n saveDispatch(() => {\n if (view) view.dispatch(view.state.update({ effects: openedImageEffect.of(undefined) }));\n });\n });\n });\n\n return false;\n}\n"],"names":[],"mappings":";;;;;AAKA,MAAM,WAAW,GAAsD,EAAE;AACzE,MAAM,cAAc,GAAG,KAAK;AAC5B,MAAM,gBAAgB,GAAgB,IAAI,GAAG,EAAE;AAC/C,IAAI,QAAQ,GAA0B,IAAI;AAOpC,MAAO,WAAY,SAAQ,UAAU,CAAA;AAE/B,IAAA,IAAA;AACA,IAAA,IAAA;AACA,IAAA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CAAC,IAAI;YAAE;QACX,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,iBAAiB,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;AAExE,QAAA,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC;QACnD,IAAI,QAAQ,EAAE;YACZ,UAAU,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;;QAGjD,qBAAqB,CAAC,MAAK;YACzB,YAAY,CAAC,MAAK;AAChB,gBAAA,IAAI,IAAI;oBAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,iBAAiB,CAAC,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;AAC1F,aAAC,CAAC;AACJ,SAAC,CAAC;AACJ,KAAC,CAAC;AAEF,IAAA,OAAO,KAAK;AACd;;;;"}
|
|
@@ -12,12 +12,12 @@ function getItalicDecorations({ decorations, node }) {
|
|
|
12
12
|
range: [node.from, node.to],
|
|
13
13
|
}));
|
|
14
14
|
}
|
|
15
|
-
function getItalicSelectionDecorations({ decorations, node, view, forceActive, }) {
|
|
15
|
+
function getItalicSelectionDecorations({ decorations, node, view, forceActive, settings, }) {
|
|
16
16
|
if (node.name !== NAME_OF_ITALIC) {
|
|
17
17
|
return;
|
|
18
18
|
}
|
|
19
|
-
if (checkIsSeveralEmphasis({ decorations, node, view, forceActive })) {
|
|
20
|
-
return void splitEmphasis({ decorations, node, view, forceActive });
|
|
19
|
+
if (checkIsSeveralEmphasis({ decorations, node, view, forceActive, settings })) {
|
|
20
|
+
return void splitEmphasis({ decorations, node, view, forceActive, settings });
|
|
21
21
|
}
|
|
22
22
|
let step = 1;
|
|
23
23
|
const startText = view.state.doc.sliceString(node.from, node.from + 3);
|
|
@@ -46,7 +46,7 @@ function checkIsSeveralEmphasis({ node, view }) {
|
|
|
46
46
|
return true;
|
|
47
47
|
return false;
|
|
48
48
|
}
|
|
49
|
-
function splitEmphasis({ decorations, node, view, forceActive }) {
|
|
49
|
+
function splitEmphasis({ decorations, node, view, forceActive, settings, }) {
|
|
50
50
|
const text = view.state.doc.sliceString(node.from, node.to);
|
|
51
51
|
let marks = 0;
|
|
52
52
|
let pos = 0;
|
|
@@ -60,12 +60,14 @@ function splitEmphasis({ decorations, node, view, forceActive }) {
|
|
|
60
60
|
view,
|
|
61
61
|
node: { ...node, name: node.name, from: node.from, to: node.from + pos },
|
|
62
62
|
forceActive,
|
|
63
|
+
settings,
|
|
63
64
|
});
|
|
64
65
|
getItalicSelectionDecorations({
|
|
65
66
|
decorations,
|
|
66
67
|
view,
|
|
67
68
|
node: { ...node, name: node.name, from: node.from + pos, to: node.to },
|
|
68
69
|
forceActive,
|
|
70
|
+
settings,
|
|
69
71
|
});
|
|
70
72
|
}
|
|
71
73
|
const italicDecorationPlugin = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"italic-decoration.js","sources":["../../../../../src/extensions/markdown/italic/italic-decoration.ts"],"sourcesContent":["import { utils } from \"@/lib\";\nimport type {\n DecorationPlugin,\n GetDecorationOptions,\n GetSelectionDecorationOptions,\n} from \"../markdown-types\";\nimport styles from \"../styles.module.scss\";\nimport { LIST_OF_ITALIC_MARKS, NAME_OF_ITALIC } from \"./italic-constants\";\n\nfunction getItalicDecorations({ decorations, node }: GetDecorationOptions) {\n if (node.name !== NAME_OF_ITALIC) {\n return;\n }\n\n decorations.push(\n utils.getMarkDecoration({\n style: styles.italic,\n range: [node.from, node.to],\n }),\n );\n}\n\nfunction getItalicSelectionDecorations({\n decorations,\n node,\n view,\n forceActive,\n}: GetSelectionDecorationOptions) {\n if (node.name !== NAME_OF_ITALIC) {\n return;\n }\n\n if (checkIsSeveralEmphasis({ decorations, node, view, forceActive })) {\n return void splitEmphasis({ decorations, node, view, forceActive });\n }\n\n let step = 1;\n const startText = view.state.doc.sliceString(node.from, node.from + 3);\n if (\n LIST_OF_ITALIC_MARKS.has(startText.charCodeAt(0)) &&\n LIST_OF_ITALIC_MARKS.has(startText.charCodeAt(1)) &&\n LIST_OF_ITALIC_MARKS.has(startText.charCodeAt(2))\n )\n step = 3;\n\n if (\n forceActive ||\n !view.hasFocus ||\n !utils.isInRange(view.state.selection.ranges, [node.from, node.to])\n ) {\n decorations.push(utils.getHideDecoration({ range: [node.from, node.from + step] }));\n decorations.push(utils.getHideDecoration({ range: [node.to - step, node.to] }));\n }\n}\n\n/** Fixed wide italic + italic */\nfunction checkIsSeveralEmphasis({ node, view }: GetSelectionDecorationOptions) {\n let marks = 0;\n let pos = 0;\n\n const text = view.state.doc.sliceString(node.from, node.to);\n\n while (pos <= text.length) {\n if (LIST_OF_ITALIC_MARKS.has(text.charCodeAt(pos))) marks++;\n pos++;\n }\n\n if (marks === 8) return true;\n\n return false;\n}\n\nfunction splitEmphasis({
|
|
1
|
+
{"version":3,"file":"italic-decoration.js","sources":["../../../../../src/extensions/markdown/italic/italic-decoration.ts"],"sourcesContent":["import { utils } from \"@/lib\";\nimport type {\n DecorationPlugin,\n GetDecorationOptions,\n GetSelectionDecorationOptions,\n} from \"../markdown-types\";\nimport styles from \"../styles.module.scss\";\nimport { LIST_OF_ITALIC_MARKS, NAME_OF_ITALIC } from \"./italic-constants\";\n\nfunction getItalicDecorations({ decorations, node }: GetDecorationOptions) {\n if (node.name !== NAME_OF_ITALIC) {\n return;\n }\n\n decorations.push(\n utils.getMarkDecoration({\n style: styles.italic,\n range: [node.from, node.to],\n }),\n );\n}\n\nfunction getItalicSelectionDecorations({\n decorations,\n node,\n view,\n forceActive,\n settings,\n}: GetSelectionDecorationOptions) {\n if (node.name !== NAME_OF_ITALIC) {\n return;\n }\n\n if (checkIsSeveralEmphasis({ decorations, node, view, forceActive, settings })) {\n return void splitEmphasis({ decorations, node, view, forceActive, settings });\n }\n\n let step = 1;\n const startText = view.state.doc.sliceString(node.from, node.from + 3);\n if (\n LIST_OF_ITALIC_MARKS.has(startText.charCodeAt(0)) &&\n LIST_OF_ITALIC_MARKS.has(startText.charCodeAt(1)) &&\n LIST_OF_ITALIC_MARKS.has(startText.charCodeAt(2))\n )\n step = 3;\n\n if (\n forceActive ||\n !view.hasFocus ||\n !utils.isInRange(view.state.selection.ranges, [node.from, node.to])\n ) {\n decorations.push(utils.getHideDecoration({ range: [node.from, node.from + step] }));\n decorations.push(utils.getHideDecoration({ range: [node.to - step, node.to] }));\n }\n}\n\n/** Fixed wide italic + italic */\nfunction checkIsSeveralEmphasis({ node, view }: GetSelectionDecorationOptions) {\n let marks = 0;\n let pos = 0;\n\n const text = view.state.doc.sliceString(node.from, node.to);\n\n while (pos <= text.length) {\n if (LIST_OF_ITALIC_MARKS.has(text.charCodeAt(pos))) marks++;\n pos++;\n }\n\n if (marks === 8) return true;\n\n return false;\n}\n\nfunction splitEmphasis({\n decorations,\n node,\n view,\n forceActive,\n settings,\n}: GetSelectionDecorationOptions) {\n const text = view.state.doc.sliceString(node.from, node.to);\n let marks = 0;\n let pos = 0;\n\n while (pos <= text.length && marks < 6) {\n if (LIST_OF_ITALIC_MARKS.has(text.charCodeAt(pos))) marks++;\n pos++;\n }\n\n getItalicSelectionDecorations({\n decorations,\n view,\n node: { ...node, name: node.name, from: node.from, to: node.from + pos },\n forceActive,\n settings,\n });\n getItalicSelectionDecorations({\n decorations,\n view,\n node: { ...node, name: node.name, from: node.from + pos, to: node.to },\n forceActive,\n settings,\n });\n}\n\nexport const italicDecorationPlugin: DecorationPlugin = {\n decorations: [getItalicDecorations],\n selectionDecorations: [getItalicSelectionDecorations],\n};\n"],"names":["utils.getMarkDecoration","utils.isInRange","utils.getHideDecoration"],"mappings":";;;;;AASA,SAAS,oBAAoB,CAAC,EAAE,WAAW,EAAE,IAAI,EAAwB,EAAA;AACvE,IAAA,IAAI,IAAI,CAAC,IAAI,KAAK,cAAc,EA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KAAK,EAAE;AAC3D,QAAA,GAAG,EAAE;;AAGP,IAAA,6BAA6B,CAAC;QAC5B,WAAW;QACX,IAAI;QACJ,IAAI,EAAE,EAAE,GAAG,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,EAAE,IAAI,CAAC,IAAI,GAAG,GAAG,EAAE;QACxE,WAAW;QACX,QAAQ;AACT,KAAA,CAAC;AACF,IAAA,6BAA6B,CAAC;QAC5B,WAAW;QACX,IAAI;QACJ,IAAI,EAAE,EAAE,GAAG,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,GAAG,GAAG,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE;QACtE,WAAW;QACX,QAAQ;AACT,KAAA,CAAC;AACJ;AAEa,MAAA,sBAAsB,GAAqB;IACtD,WAAW,EAAE,CAAC,oBAAoB,CAAC;IACnC,oBAAoB,EAAE,CAAC,6BAA6B,CAAC;;;;;"}
|
|
@@ -9,7 +9,8 @@ function getAutoLinkSelectionDecorations({ decorations, node, view, forceActive,
|
|
|
9
9
|
return;
|
|
10
10
|
const url = view.state.doc.sliceString(node.from + 1, node.to - 1);
|
|
11
11
|
const openedLink = view.state.field(markdownState).openedLink;
|
|
12
|
-
const
|
|
12
|
+
const uniqueId = view.state.field(markdownState).uniqueId;
|
|
13
|
+
const key = `${url}:${url}:${uniqueId}:${node.from}:${node.to}`;
|
|
13
14
|
const isOpened = openedLink && openedLink === key;
|
|
14
15
|
if (isOpened) {
|
|
15
16
|
return void decorations.push(getMarkDecoration({
|
|
@@ -24,7 +25,7 @@ function getAutoLinkSelectionDecorations({ decorations, node, view, forceActive,
|
|
|
24
25
|
!isInRange(view.state.selection.ranges, [node.from, node.to])) {
|
|
25
26
|
decorations.push(getReplaceDecoration({
|
|
26
27
|
range: [node.from, node.to],
|
|
27
|
-
widget: new LinkWidget(url, url, node.from, node.to, view),
|
|
28
|
+
widget: new LinkWidget(url, url, node.from, node.to, uniqueId, view),
|
|
28
29
|
}));
|
|
29
30
|
}
|
|
30
31
|
else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auto-link-decoration.js","sources":["../../../../../src/extensions/markdown/link/auto-link-decoration.ts"],"sourcesContent":["import { utils } from \"@/lib\";\nimport { markdownState } from \"../markdown-state\";\nimport type { DecorationPlugin, GetSelectionDecorationOptions } from \"../markdown-types\";\nimport { NAME_OF_AUTO_LINK } from \"./link-constants\";\nimport { LinkWidget } from \"./link-widget\";\n\nfunction getAutoLinkSelectionDecorations({\n decorations,\n node,\n view,\n forceActive,\n}: GetSelectionDecorationOptions) {\n if (node.name !== NAME_OF_AUTO_LINK) return;\n\n const url = view.state.doc.sliceString(node.from + 1, node.to - 1);\n const openedLink = view.state.field(markdownState).openedLink;\n const key = `${url}:${url}:${node.from}:${node.to}`;\n const isOpened = openedLink && openedLink === key;\n\n if (isOpened) {\n return void decorations.push(\n utils.getMarkDecoration({\n range: [node.from, node.to],\n attributes: {\n \"data-id\": key,\n },\n }),\n );\n }\n\n if (\n forceActive ||\n !view.hasFocus ||\n !utils.isInRange(view.state.selection.ranges, [node.from, node.to])\n ) {\n decorations.push(\n utils.getReplaceDecoration({\n range: [node.from, node.to],\n widget: new LinkWidget(url, url, node.from, node.to, view),\n }),\n );\n } else {\n decorations.push(\n utils.getMarkDecoration({\n range: [node.from, node.to],\n attributes: {\n \"data-id\": key,\n },\n }),\n );\n }\n}\n\nexport const autoLinkDecorationPlugin: DecorationPlugin = {\n selectionDecorations: [getAutoLinkSelectionDecorations],\n};\n"],"names":["utils.getMarkDecoration","utils.isInRange","utils.getReplaceDecoration"],"mappings":";;;;;;AAMA,SAAS,+BAA+B,CAAC,EACvC,WAAW,EACX,IAAI,EACJ,IAAI,EACJ,WAAW,GACmB,EAAA;AAC9B,IAAA,IAAI,IAAI,CAAC,IAAI,KAAK,iBAAiB;QAAE;IAErC,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,EAAE,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC;AAClE,IAAA,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC,UAAU;AAC7D,IAAA,MAAM,
|
|
1
|
+
{"version":3,"file":"auto-link-decoration.js","sources":["../../../../../src/extensions/markdown/link/auto-link-decoration.ts"],"sourcesContent":["import { utils } from \"@/lib\";\nimport { markdownState } from \"../markdown-state\";\nimport type { DecorationPlugin, GetSelectionDecorationOptions } from \"../markdown-types\";\nimport { NAME_OF_AUTO_LINK } from \"./link-constants\";\nimport { LinkWidget } from \"./link-widget\";\n\nfunction getAutoLinkSelectionDecorations({\n decorations,\n node,\n view,\n forceActive,\n}: GetSelectionDecorationOptions) {\n if (node.name !== NAME_OF_AUTO_LINK) return;\n\n const url = view.state.doc.sliceString(node.from + 1, node.to - 1);\n const openedLink = view.state.field(markdownState).openedLink;\n const uniqueId = view.state.field(markdownState).uniqueId;\n const key = `${url}:${url}:${uniqueId}:${node.from}:${node.to}`;\n const isOpened = openedLink && openedLink === key;\n\n if (isOpened) {\n return void decorations.push(\n utils.getMarkDecoration({\n range: [node.from, node.to],\n attributes: {\n \"data-id\": key,\n },\n }),\n );\n }\n\n if (\n forceActive ||\n !view.hasFocus ||\n !utils.isInRange(view.state.selection.ranges, [node.from, node.to])\n ) {\n decorations.push(\n utils.getReplaceDecoration({\n range: [node.from, node.to],\n widget: new LinkWidget(url, url, node.from, node.to, uniqueId, view),\n }),\n );\n } else {\n decorations.push(\n utils.getMarkDecoration({\n range: [node.from, node.to],\n attributes: {\n \"data-id\": key,\n },\n }),\n );\n }\n}\n\nexport const autoLinkDecorationPlugin: DecorationPlugin = {\n selectionDecorations: [getAutoLinkSelectionDecorations],\n};\n"],"names":["utils.getMarkDecoration","utils.isIn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zB,KAAK,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC;AAC3B,YAAA,MAAM,EAAE,IAAI,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,CAAC;AACrE,SAAA,CAAC,CACH;;SACI;AACL,QAAA,WAAW,CAAC,IAAI,CACdF,iBAAuB,CAAC;YACtB,KAAK,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC;AAC3B,YAAA,UAAU,EAAE;AACV,gBAAA,SAAS,EAAE,GAAG;AACf,aAAA;AACF,SAAA,CAAC,CACH;;AAEL;AAEa,MAAA,wBAAwB,GAAqB;IACxD,oBAAoB,EAAE,CAAC,+BAA+B,CAAC;;;;;"}
|
|
@@ -5,7 +5,7 @@ import { NAME_OF_LINK, CODE_OF_START_LINK_TEXT, CODE_OF_START_LINK_URL, CODE_OF_
|
|
|
5
5
|
import { getLinkLabelSelectionDecoration } from './link-label-decoration.js';
|
|
6
6
|
import { LinkWidget } from './link-widget.js';
|
|
7
7
|
|
|
8
|
-
function getLinkSelectionDecorations({ decorations, node, view, forceActive, }) {
|
|
8
|
+
function getLinkSelectionDecorations({ decorations, node, view, forceActive, settings, }) {
|
|
9
9
|
if (node.name !== NAME_OF_LINK) {
|
|
10
10
|
return;
|
|
11
11
|
}
|
|
@@ -32,11 +32,12 @@ function getLinkSelectionDecorations({ decorations, node, view, forceActive, })
|
|
|
32
32
|
urlCoordinates.to = pos;
|
|
33
33
|
}
|
|
34
34
|
if (urlCoordinates.from === -1 || urlCoordinates.to === -1)
|
|
35
|
-
return void getLinkLabelSelectionDecoration({ decorations, forceActive, node, view });
|
|
35
|
+
return void getLinkLabelSelectionDecoration({ decorations, forceActive, node, view, settings });
|
|
36
36
|
const text = content.substring(textCoordinates.from, textCoordinates.to);
|
|
37
37
|
const url = content.substring(urlCoordinates.from, urlCoordinates.to);
|
|
38
38
|
const openedLink = view.state.field(markdownState).openedLink;
|
|
39
|
-
const
|
|
39
|
+
const uniqueId = view.state.field(markdownState).uniqueId;
|
|
40
|
+
const key = `${url}:${text}:${uniqueId}:${node.from}:${node.to}`;
|
|
40
41
|
const isOpened = openedLink && openedLink === key;
|
|
41
42
|
if (isOpened) {
|
|
42
43
|
return void decorations.push(getMarkDecoration({
|
|
@@ -51,7 +52,7 @@ function getLinkSelectionDecorations({ decorations, node, view, forceActive, })
|
|
|
51
52
|
!isInRange(view.state.selection.ranges, [node.from, node.to])) {
|
|
52
53
|
decorations.push(getReplaceDecoration({
|
|
53
54
|
range: [node.from, node.to],
|
|
54
|
-
widget: new LinkWidget(text, url, node.from, node.to, view),
|
|
55
|
+
widget: new LinkWidget(text, url, node.from, node.to, uniqueId, view),
|
|
55
56
|
}));
|
|
56
57
|
}
|
|
57
58
|
else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"link-decoration.js","sources":["../../../../../src/extensions/markdown/link/link-decoration.ts"],"sourcesContent":["import { utils } from \"@/lib\";\nimport { markdownState } from \"../markdown-state\";\nimport type { DecorationPlugin, GetSelectionDecorationOptions } from \"../markdown-types\";\nimport {\n CODE_OF_END_LINK_TEXT,\n CODE_OF_END_LINK_URL,\n CODE_OF_START_LINK_TEXT,\n CODE_OF_START_LINK_URL,\n NAME_OF_LINK,\n} from \"./link-constants\";\nimport { getLinkLabelSelectionDecoration } from \"./link-label-decoration\";\nimport { LinkWidget } from \"./link-widget\";\n\nfunction getLinkSelectionDecorations({\n decorations,\n node,\n view,\n forceActive,\n}: GetSelectionDecorationOptions) {\n if (node.name !== NAME_OF_LINK) {\n return;\n }\n\n const content = view.state.doc.sliceString(node.from, node.to);\n const textCoordinates = { from: -1, to: -1 };\n const urlCoordinates = { from: -1, to: -1 };\n let pos = -1;\n\n while (pos < content.length) {\n pos++;\n const code = content.charCodeAt(pos);\n\n if (textCoordinates.from === -1 && code === CODE_OF_START_LINK_TEXT)\n textCoordinates.from = pos + 1;\n else if (\n urlCoordinates.from === -1 &&\n textCoordinates.to !== -1 &&\n code === CODE_OF_START_LINK_URL\n )\n urlCoordinates.from = pos + 1;\n else if (\n textCoordinates.from !== -1 &&\n textCoordinates.to === -1 &&\n code === CODE_OF_END_LINK_TEXT\n )\n textCoordinates.to = pos;\n else if (\n urlCoordinates.from !== -1 &&\n urlCoordinates.to === -1 &&\n code === CODE_OF_END_LINK_URL\n )\n urlCoordinates.to = pos;\n }\n\n if (urlCoordinates.from === -1 || urlCoordinates.to === -1)\n return void getLinkLabelSelectionDecoration({ decorations, forceActive, node, view });\n\n const text = content.substring(textCoordinates.from, textCoordinates.to);\n const url = content.substring(urlCoordinates.from, urlCoordinates.to);\n\n const openedLink = view.state.field(markdownState).openedLink;\n const key = `${url}:${text}:${node.from}:${node.to}`;\n const isOpened = openedLink && openedLink === key;\n\n if (isOpened) {\n return void decorations.push(\n utils.getMarkDecoration({\n range: [node.from, node.to],\n attributes: {\n \"data-id\": key,\n },\n }),\n );\n }\n\n if (\n forceActive ||\n !view.hasFocus ||\n !utils.isInRange(view.state.selection.ranges, [node.from, node.to])\n ) {\n decorations.push(\n utils.getReplaceDecoration({\n range: [node.from, node.to],\n widget: new LinkWidget(text, url, node.from, node.to, view),\n }),\n );\n } else {\n decorations.push(\n utils.getMarkDecoration({\n range: [node.from, node.to],\n attributes: {\n \"data-id\": key,\n },\n }),\n );\n }\n}\n\nexport const linkDecorationPlugin: DecorationPlugin = {\n selectionDecorations: [getLinkSelectionDecorations],\n};\n"],"names":["utils.getMarkDecoration","utils.isInRange","utils.getReplaceDecoration"],"mappings":";;;;;;;AAaA,SAAS,2BAA2B,CAAC,EACnC,WAAW,EACX,IAAI,EACJ,IAAI,EACJ,WAAW,
|
|
1
|
+
{"version":3,"file":"link-decoration.js","sources":["../../../../../src/extensions/markdown/link/link-decoration.ts"],"sourcesContent":["import { utils } from \"@/lib\";\nimport { markdownState } from \"../markdown-state\";\nimport type { DecorationPlugin, GetSelectionDecorationOptions } from \"../markdown-types\";\nimport {\n CODE_OF_END_LINK_TEXT,\n CODE_OF_END_LINK_URL,\n CODE_OF_START_LINK_TEXT,\n CODE_OF_START_LINK_URL,\n NAME_OF_LINK,\n} from \"./link-constants\";\nimport { getLinkLabelSelectionDecoration } from \"./link-label-decoration\";\nimport { LinkWidget } from \"./link-widget\";\n\nfunction getLinkSelectionDecorations({\n decorations,\n node,\n view,\n forceActive,\n settings,\n}: GetSelectionDecorationOptions) {\n if (node.name !== NAME_OF_LINK) {\n return;\n }\n\n const content = view.state.doc.sliceString(node.from, node.to);\n const textCoordinates = { from: -1, to: -1 };\n const urlCoordinates = { from: -1, to: -1 };\n let pos = -1;\n\n while (pos < content.length) {\n pos++;\n const code = content.charCodeAt(pos);\n\n if (textCoordinates.from === -1 && code === CODE_OF_START_LINK_TEXT)\n textCoordinates.from = pos + 1;\n else if (\n urlCoordinates.from === -1 &&\n textCoordinates.to !== -1 &&\n code === CODE_OF_START_LINK_URL\n )\n urlCoordinates.from = pos + 1;\n else if (\n textCoordinates.from !== -1 &&\n textCoordinates.to === -1 &&\n code === CODE_OF_END_LINK_TEXT\n )\n textCoordinates.to = pos;\n else if (\n urlCoordinates.from !== -1 &&\n urlCoordinates.to === -1 &&\n code === CODE_OF_END_LINK_URL\n )\n urlCoordinates.to = pos;\n }\n\n if (urlCoordinates.from === -1 || urlCoordinates.to === -1)\n return void getLinkLabelSelectionDecoration({ decorations, forceActive, node, view, settings });\n\n const text = content.substring(textCoordinates.from, textCoordinates.to);\n const url = content.substring(urlCoordinates.from, urlCoordinates.to);\n\n const openedLink = view.state.field(markdownState).openedLink;\n const uniqueId = view.state.field(markdownState).uniqueId;\n const key = `${url}:${text}:${uniqueId}:${node.from}:${node.to}`;\n const isOpened = openedLink && openedLink === key;\n\n if (isOpened) {\n return void decorations.push(\n utils.getMarkDecoration({\n range: [node.from, node.to],\n attributes: {\n \"data-id\": key,\n },\n }),\n );\n }\n\n if (\n forceActive ||\n !view.hasFocus ||\n !utils.isInRange(view.state.selection.ranges, [node.from, node.to])\n ) {\n decorations.push(\n utils.getReplaceDecoration({\n range: [node.from, node.to],\n widget: new LinkWidget(text, url, node.from, node.to, uniqueId, view),\n }),\n );\n } else {\n decorations.push(\n utils.getMarkDecoration({\n range: [node.from, node.to],\n attributes: {\n \"data-id\": key,\n },\n }),\n );\n }\n}\n\nexport const linkDecorationPlugin: DecorationPlugin = {\n selectionDecorations: [getLinkSelectionDecorations],\n};\n"],"names":["utils.getMarkDecoration","utils.isInRange","utils.getReplaceDecoration"],"mappings":";;;;;;;AAaA,SAAS,2BAA2B,CAAC,EACnC,WAAW,EACX,IAAI,EACJ,IAAI,EACJ,WAAW,EACX,QAAQ,GACsB,EAAA;AAC9B,IAAA,IAAI,IAAI,CAAC,IAAI,KAAK,YAAY,EAAE;QAC9B;;AAGF,IAAA,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC;AAC9D,IAAA,MAAM,eAAe,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE;AAC5C,IAAA,MAAM,cAAc,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE;AAC3C,IAAA,IAAI,GAAG,GAAG,CAAC,CAAC;AAEZ,IAAA,OAAO,GAAG,GAAG,OAAO,CAAC,MAAM,EAAE;AAC3B,QAAA,GAAG,EAAE;QACL,MAAM,IAAI,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC;QAEpC,IAAI,eAAe,CAAC,IAAI,KAAK,CAAC,CAAC,IAAI,IAAI,KAAK,uBAAuB;AACjE,YAAA,eAAe,CAAC,IAAI,GAAG,GAAG,GAAG,CAAC;AAC3B,aAAA,IACH,cAAc,CAAC,IAAI,KAAK,CAAC,CAAC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Id;
|
|
16
17
|
view;
|
|
17
|
-
constructor(text, link, from, to, view) {
|
|
18
|
+
constructor(text, link, from, to, uniqueId, view) {
|
|
18
19
|
super();
|
|
19
20
|
this.text = text;
|
|
20
21
|
this.link = link;
|
|
21
22
|
this.from = from;
|
|
22
23
|
this.to = to;
|
|
24
|
+
this.uniqueId = uniqueId;
|
|
23
25
|
this.view = view;
|
|
24
26
|
}
|
|
25
27
|
get key() {
|
|
26
|
-
return `${this.link}:${this.text}:${this.from}:${this.to}`;
|
|
28
|
+
return `${this.link}:${this.text}:${this.uniqueId}:${this.from}:${this.to}`;
|
|
27
29
|
}
|
|
28
30
|
eq(widget) {
|
|
29
31
|
const anchor = LINK_NODES[this.key];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"link-widget.js","sources":["../../../../../src/extensions/markdown/link/link-widget.ts"],"sourcesContent":["import { type EditorView, WidgetType } from \"@codemirror/view\";\nimport { CLASSES } from \"@/extensions/theme\";\nimport { saveDispatch } from \"@/lib/utils\";\nimport { openedLinkEffect } from \"../markdown-state\";\nimport styles from \"../styles.module.scss\";\n\nconst LINK_NODES: Record<string, AnchorElement | undefined> = {};\n\ninterface AnchorElement extends HTMLAnchorElement {\n clearListeners?: () => void;\n destroy?: () => void;\n}\nexport class LinkWidget extends WidgetType {\n constructor(\n private text: string,\n private link: string,\n private from: number,\n private to: number,\n private view: EditorView,\n ) {\n super();\n }\n\n get key() {\n return `${this.link}:${this.text}:${this.from}:${this.to}`;\n }\n\n eq(widget: LinkWidget): boolean {\n const anchor = LINK_NODES[this.key];\n if (!anchor) return false;\n delete LINK_NODES[this.key];\n\n if (anchor.href !== widget.link) anchor.href = widget.link;\n if (anchor.textContent !== widget.text) anchor.textContent = widget.text;\n\n this.link = widget.link;\n this.text = widget.text;\n this.from = widget.from;\n this.to = widget.to;\n\n this.registerListeners(anchor);\n LINK_NODES[this.key] = anchor;\n\n return true;\n }\n\n toDOM(): HTMLElement {\n const anchor = document.createElement(\"a\") as AnchorElement;\n anchor.classList.add(styles.link);\n anchor.classList.add(CLASSES.link);\n\n anchor.target = \"_blank\";\n anchor.textContent = this.text;\n anchor.href = this.link;\n\n this.registerListeners(anchor);\n\n LINK_NODES[this.key] = anchor;\n\n return anchor;\n }\n\n destroy(dom: AnchorElement): void {\n delete LINK_NODES[this.key];\n dom.destroy?.();\n }\n\n registerListeners(anchor: AnchorElement) {\n anchor.clearListeners?.();\n const abortController = new AbortController();\n anchor.addEventListener(\n \"mousedown\",\n (event) => handleClick(this.view, this.text, this.link, this.key, event),\n { signal: abortController.signal },\n );\n anchor.addEventListener(\"click\", (e) => e.preventDefault(), { signal: abortController.signal });\n anchor.clearListeners = () => {\n abortController.abort();\n };\n anchor.destroy = () => {\n anchor.clearListeners?.();\n anchor.remove();\n };\n }\n}\n\n/** recursively find the link text node in line */\nfunction getTextNode(\n text: string,\n link: string,\n key: string,\n line: ChildNode | Node | null | undefined,\n): ChildNode | null {\n if (!line) return null;\n const textNodeContainer = getTextNodeContainer(text, link, key, line);\n if (!textNodeContainer) return null;\n\n for (const node of Array.from(textNodeContainer.childNodes)) {\n if (isCorrectNode(text, link, node)) {\n return node;\n }\n }\n\n return null;\n}\n\nfunction getTextNodeContainer(\n text: string,\n link: string,\n key: string,\n line: ChildNode | Node | null | undefined,\n): HTMLElement | null {\n if (!line) return null;\n\n for (const node of Array.from(line.childNodes)) {\n if (node instanceof HTMLElement && node.getAttribute(\"data-id\") === key) {\n return node;\n }\n\n if (node.nodeType !== 3) {\n const inner = getTextNodeContainer(text, link, key, node);\n if (inner) return inner;\n }\n }\n\n return null;\n}\n\nfunction isCorrectNode(\n text: string,\n link: string,\n node: ChildNode | Node | null | undefined,\n): node is ChildNode | Node {\n if (!node) return false;\n\n const textContent = node?.textContent;\n\n return Boolean(\n node &&\n textContent &&\n node.nodeType === 3 &&\n textContent.includes(link) &&\n textContent.includes(text),\n );\n}\n\ntype SelectLinkOptions = {\n node: ChildNode | Node;\n selection: Selection;\n start?: number;\n link: string;\n};\nfunction selectLink({ link, node, selection, start }: SelectLinkOptions) {\n const startPosition = start ?? (node.textContent?.indexOf?.(link) || 0);\n const endPosition = startPosition + link.length;\n\n const range = document.createRange();\n range.setStart(node, startPosition);\n range.setEnd(node, endPosition);\n selection.removeAllRanges();\n selection.addRange(range);\n}\n\nfunction handleClick(view: EditorView, text: string, link: string, key: string, event: MouseEvent) {\n /** open the link if has special key or the view is readonly */\n const contentEditable = view.contentDOM.getAttribute(\"contenteditable\");\n const forceActive = !contentEditable || contentEditable === \"false\";\n\n if (event.shiftKey || event.ctrlKey || event.altKey || event.metaKey || forceActive) {\n if (event.type === \"mousedown\") {\n const target = event.target as HTMLAnchorElement;\n window.open(target.href, \"_blank\");\n }\n\n return;\n }\n\n event.stopPropagation();\n event.preventDefault();\n const target = event.target as HTMLImageElement;\n const parent = target.parentNode;\n let line: HTMLElement | null = parent as HTMLElement | null;\n\n /** recursively find line that contains link */\n while (line && !line.classList.contains(\"cm-line\")) {\n line = line.parentNode as HTMLElement | null;\n }\n\n const editor = Array.from(document.querySelectorAll(\".cm-editor\")).find((element) =>\n element.contains(target),\n );\n const selection = window.getSelection();\n\n if (!selection || !editor || !parent) return;\n\n const textNode = getTextNode(text, link, key, line);\n\n if (textNode) {\n return void selectLink({ selection, link, node: textNode });\n }\n\n saveDispatch(() => {\n if (!view) return;\n\n view.dispatch(view.state.update({ effects: openedLinkEffect.of(key) }));\n\n const textNode = getTextNode(text, link, key, line);\n if (textNode) {\n selectLink({ selection, link, node: textNode });\n }\n\n requestAnimationFrame(() => {\n saveDispatch(() => {\n if (view) view.dispatch(view.state.update({ effects: openedLinkEffect.of(undefined) }));\n });\n });\n });\n\n return false;\n}\n"],"names":[],"mappings":";;;;;;;;;AAMA,MAAM,UAAU,GAA8C,EAAE;AAM1D,MAAO,UAAW,SAAQ,UAAU,CAAA;AAE9B,IAAA,IAAA;AACA,IAAA,IAAA;AACA,IAAA,IAAA;AACA,IAAA,EAAA;AACA,IAAA,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AAC,CAAC;AAEvE,QAAA,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC;QACnD,IAAI,QAAQ,EAAE;YACZ,UAAU,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;;QAGjD,qBAAqB,CAAC,MAAK;YACzB,YAAY,CAAC,MAAK;AAChB,gBAAA,IAAI,IAAI;oBAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,gBAAgB,CAAC,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;AACzF,aAAC,CAAC;AACJ,SAAC,CAAC;AACJ,KAAC,CAAC;AAEF,IAAA,OAAO,KAAK;AACd;;;;"}
|
|
1
|
+
{"version":3,"file":"link-widget.js","sources":["../../../../../src/extensions/markdown/link/link-widget.ts"],"sourcesContent":["import { type EditorView, WidgetType } from \"@codemirror/view\";\nimport { CLASSES } from \"@/extensions/theme\";\nimport { saveDispatch } from \"@/lib/utils\";\nimport { openedLinkEffect } from \"../markdown-state\";\nimport styles from \"../styles.module.scss\";\n\nconst LINK_NODES: Record<string, AnchorElement | undefined> = {};\n\ninterface AnchorElement extends HTMLAnchorElement {\n clearListeners?: () => void;\n destroy?: () => void;\n}\nexport class LinkWidget extends WidgetType {\n constructor(\n private text: string,\n private link: string,\n private from: number,\n private to: number,\n private uniqueId: string,\n private view: EditorView,\n ) {\n super();\n }\n\n get key() {\n return `${this.link}:${this.text}:${this.uniqueId}:${this.from}:${this.to}`;\n }\n\n eq(widget: LinkWidget): boolean {\n const anchor = LINK_NODES[this.key];\n if (!anchor) return false;\n delete LINK_NODES[this.key];\n\n if (anchor.href !== widget.link) anchor.href = widget.link;\n if (anchor.textContent !== widget.text) anchor.textContent = widget.text;\n\n this.link = widget.link;\n this.text = widget.text;\n this.from = widget.from;\n this.to = widget.to;\n\n this.registerListeners(anchor);\n LINK_NODES[this.key] = anchor;\n\n return true;\n }\n\n toDOM(): HTMLElement {\n const anchor = document.createElement(\"a\") as AnchorElement;\n anchor.classList.add(styles.link);\n anchor.classList.add(CLASSES.link);\n\n anchor.target = \"_blank\";\n anchor.textContent = this.text;\n anchor.href = this.link;\n\n this.registerListeners(anchor);\n\n LINK_NODES[this.key] = anchor;\n\n return anchor;\n }\n\n destroy(dom: AnchorElement): void {\n delete LINK_NODES[this.key];\n dom.destroy?.();\n }\n\n registerListeners(anchor: AnchorElement) {\n anchor.clearListeners?.();\n const abortController = new AbortController();\n anchor.addEventListener(\n \"mousedown\",\n (event) => handleClick(this.view, this.text, this.link, this.key, event),\n { signal: abortController.signal },\n );\n anchor.addEventListener(\"click\", (e) => e.preventDefault(), { signal: abortController.signal });\n anchor.clearListeners = () => {\n abortController.abort();\n };\n anchor.destroy = () => {\n anchor.clearListeners?.();\n anchor.remove();\n };\n }\n}\n\n/** recursively find the link text node in line */\nfunction getTextNode(\n text: string,\n link: string,\n key: string,\n line: ChildNode | Node | null | undefined,\n): ChildNode | null {\n if (!line) return null;\n const textNodeContainer = getTextNodeContainer(text, link, key, line);\n if (!textNodeContainer) return null;\n\n for (const node of Array.from(textNodeContainer.childNodes)) {\n if (isCorrectNode(text, link, node)) {\n return node;\n }\n }\n\n return null;\n}\n\nfunction getTextNodeContainer(\n text: string,\n link: string,\n key: string,\n line: ChildNode | Node | null | undefined,\n): HTMLElement | null {\n if (!line) return null;\n\n for (const node of Array.from(line.childNodes)) {\n if (node instanceof HTMLElement && node.getAttribute(\"data-id\") === key) {\n return node;\n }\n\n if (node.nodeType !== 3) {\n const inner = getTextNodeContainer(text, link, key, node);\n if (inner) return inner;\n }\n }\n\n return null;\n}\n\nfunction isCorrectNode(\n text: string,\n link: string,\n node: ChildNode | Node | null | undefined,\n): node is ChildNode | Node {\n if (!node) return false;\n\n const textContent = node?.textContent;\n\n return Boolean(\n node &&\n textContent &&\n node.nodeType === 3 &&\n textContent.includes(link) &&\n textContent.includes(text),\n );\n}\n\ntype SelectLinkOptions = {\n node: ChildNode | Node;\n selection: Selection;\n start?: number;\n link: string;\n};\nfunction selectLink({ link, node, selection, start }: SelectLinkOptions) {\n const startPosition = start ?? (node.textContent?.indexOf?.(link) || 0);\n const endPosition = startPosition + link.length;\n\n const range = document.createRange();\n range.setStart(node, startPosition);\n range.setEnd(node, endPosition);\n selection.removeAllRanges();\n selection.addRange(range);\n}\n\nfunction handleClick(view: EditorView, text: string, link: string, key: string, event: MouseEvent) {\n /** open the link if has special key or the view is readonly */\n const contentEditable = view.contentDOM.getAttribute(\"contenteditable\");\n const forceActive = !contentEditable || contentEditable === \"false\";\n\n if (event.shiftKey || event.ctrlKey || event.altKey || event.metaKey || forceActive) {\n if (event.type === \"mousedown\") {\n const target = event.target as HTMLAnchorElement;\n window.open(target.href, \"_blank\");\n }\n\n return;\n }\n\n event.stopPropagation();\n event.preventDefault();\n const target = event.target as HTMLImageElement;\n const parent = target.parentNode;\n let line: HTMLElement | null = parent as HTMLElement | null;\n\n /** recursively find line that contains link */\n while (line && !line.classList.contains(\"cm-line\")) {\n line = line.parentNode as HTMLElement | null;\n }\n\n const editor = Array.from(document.querySelectorAll(\".cm-editor\")).find((element) =>\n element.contains(target),\n );\n const selection = window.getSelection();\n\n if (!selection || !editor || !parent) return;\n\n const textNode = getTextNode(text, link, key, line);\n\n if (textNode) {\n return void selectLink({ selection, link, node: textNode });\n }\n\n saveDispatch(() => {\n if (!view) return;\n\n view.dispatch(view.state.update({ effects: openedLinkEffect.of(key) }));\n\n const textNode = getTextNode(text, link, key, line);\n if (textNode) {\n selectLink({ selection, link, node: textNode });\n }\n\n requestAnimationFrame(() => {\n saveDispatch(() => {\n if (view) view.dispatch(view.state.update({ effects: openedLinkEffect.of(undefined) }));\n });\n });\n });\n\n return false;\n}\n"],"names":[],"mappings":";;;;;;;;;AAMA,MAAM,UAAU,GAA8C,EAAE;AAM1D,MAAO,UAAW,SAAQ,UAAU,CAAA;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;AAChB,QAAA,IAAI,CAAC,IAAI;YAAE;QAEX,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,gBAAgB,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;AAEvE,QAAA,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC;QACnD,IAAI,QAAQ,EAAE;YACZ,UAAU,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;;QAGjD,qBAAqB,CAAC,MAAK;YACzB,YAAY,CAAC,MAAK;AAChB,gBAAA,IAAI,IAAI;oBAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,gBAAgB,CAAC,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;AACzF,aAAC,CAAC;AACJ,SAAC,CAAC;AACJ,KAAC,CAAC;AAEF,IAAA,OAAO,KAAK;AACd;;;;"}
|
|
@@ -12,7 +12,6 @@ import { italicDecorationPlugin } from './italic/italic-decoration.js';
|
|
|
12
12
|
import { linkDecorationPlugin } from './link/link-decoration.js';
|
|
13
13
|
import { autoLinkDecorationPlugin } from './link/auto-link-decoration.js';
|
|
14
14
|
import { listDecorationPlugin } from './list/list-decoration.js';
|
|
15
|
-
import { imageSrcGetterEffect } from './markdown-state.js';
|
|
16
15
|
import { mentionDecorationPlugin } from './mention/mention-decoration.js';
|
|
17
16
|
import { strikeThroughDecorationPlugin } from './strike-through/strike-through-decoration.js';
|
|
18
17
|
import { todoDecorationPlugin } from './todo/todo-decoration.js';
|
|
@@ -57,7 +56,7 @@ const SKIP_MARKS = new Set([
|
|
|
57
56
|
"HeaderMark",
|
|
58
57
|
"TaskMarker",
|
|
59
58
|
]);
|
|
60
|
-
function createDecorationsGetter() {
|
|
59
|
+
function createDecorationsGetter(settings) {
|
|
61
60
|
let markdownDecorationsCache = [];
|
|
62
61
|
let markdownSelectionDecorationsCache = [];
|
|
63
62
|
function getDecorations(view, isChanged, mouseReleased) {
|
|
@@ -78,7 +77,7 @@ function createDecorationsGetter() {
|
|
|
78
77
|
return;
|
|
79
78
|
/** Decoration by change content */
|
|
80
79
|
if (processDecorations)
|
|
81
|
-
decorationFunctions.forEach((f) => f({ decorations, node, view }));
|
|
80
|
+
decorationFunctions.forEach((f) => f({ decorations, node, view, settings }));
|
|
82
81
|
/** Decoration by selection content */
|
|
83
82
|
if (processSelectionDecorations)
|
|
84
83
|
selectionDecorationFunctions.forEach((f) => f({
|
|
@@ -86,6 +85,7 @@ function createDecorationsGetter() {
|
|
|
86
85
|
node,
|
|
87
86
|
view,
|
|
88
87
|
forceActive: isReadonly,
|
|
88
|
+
settings,
|
|
89
89
|
}));
|
|
90
90
|
},
|
|
91
91
|
});
|
|
@@ -100,7 +100,7 @@ function createDecorationsGetter() {
|
|
|
100
100
|
}
|
|
101
101
|
return getDecorations;
|
|
102
102
|
}
|
|
103
|
-
const markdownDecorationPlugin = (
|
|
103
|
+
const markdownDecorationPlugin = (settings) => {
|
|
104
104
|
return ViewPlugin.fromClass(class DecorationMarkdown {
|
|
105
105
|
decorations;
|
|
106
106
|
mouseReleased = true;
|
|
@@ -108,17 +108,12 @@ const markdownDecorationPlugin = (stateConfig) => {
|
|
|
108
108
|
view;
|
|
109
109
|
decorationGetter;
|
|
110
110
|
constructor(view) {
|
|
111
|
-
this.decorationGetter = createDecorationsGetter();
|
|
111
|
+
this.decorationGetter = createDecorationsGetter(settings);
|
|
112
112
|
this.decorations = this.decorationGetter(view, true, this.mouseReleased);
|
|
113
113
|
this.dom = view.dom;
|
|
114
114
|
this.view = view;
|
|
115
115
|
document.addEventListener("mousedown", this.onMouseDown.bind(this));
|
|
116
116
|
document.addEventListener("mouseup", this.onMouseUp.bind(this));
|
|
117
|
-
saveDispatch(() => {
|
|
118
|
-
this.view.dispatch(this.view.state.update({
|
|
119
|
-
effects: [imageSrcGetterEffect.of(stateConfig.imageSrcGetter)],
|
|
120
|
-
}));
|
|
121
|
-
});
|
|
122
117
|
}
|
|
123
118
|
update(update) {
|
|
124
119
|
const isDocumentChanged = update.docChanged ||
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdown-decoration.js","sources":["../../../../src/extensions/markdown/markdown-decoration.ts"],"sourcesContent":["import { syntaxTree } from \"@codemirror/language\";\nimport { type Range } from \"@codemirror/state\";\nimport {\n Decoration,\n type DecorationSet,\n type EditorView,\n ViewPlugin,\n type ViewUpdate,\n} from \"@codemirror/view\";\nimport { saveDispatch } from \"@/lib/utils\";\nimport { blockquoteDecorationPlugin } from \"./blockquote\";\nimport { boldDecorationPlugin } from \"./bold\";\nimport { codeDecorationPlugin } from \"./code\";\nimport { headerDecorationPlugin } from \"./header\";\nimport { horizontalDecorationPlugin } from \"./horizontal\";\nimport { imageDecorationPlugin } from \"./image/image-decoration\";\nimport { italicDecorationPlugin } from \"./italic\";\nimport { autoLinkDecorationPlugin, linkDecorationPlugin } from \"./link\";\nimport { listDecorationPlugin } from \"./list\";\nimport { imageSrcGetterEffect } from \"./markdown-state\";\nimport type {\n DecorationPlugin,\n GetDecorationFunction,\n GetSelectionDecorationFunction,\n MarkdownStateConfig,\n} from \"./markdown-types\";\nimport { mentionDecorationPlugin } from \"./mention/mention-decoration\";\nimport { strikeThroughDecorationPlugin } from \"./strike-through\";\nimport { todoDecorationPlugin } from \"./todo\";\n\nconst decorationPlugins: DecorationPlugin[] = [\n blockquoteDecorationPlugin,\n boldDecorationPlugin,\n codeDecorationPlugin,\n headerDecorationPlugin,\n horizontalDecorationPlugin,\n imageDecorationPlugin,\n italicDecorationPlugin,\n linkDecorationPlugin,\n listDecorationPlugin,\n autoLinkDecorationPlugin,\n mentionDecorationPlugin,\n strikeThroughDecorationPlugin,\n todoDecorationPlugin,\n];\n\nlet decorationFunctions: GetDecorationFunction[] = [];\nlet selectionDecorationFunctions: GetSelectionDecorationFunction[] = [];\n\nfor (const plugin of decorationPlugins) {\n if (plugin.decorations) decorationFunctions = decorationFunctions.concat(plugin.decorations);\n if (plugin.selectionDecorations)\n selectionDecorationFunctions = selectionDecorationFunctions.concat(plugin.selectionDecorations);\n}\n\nconst SKIP_MARKS = new Set([\n \"Document\",\n \"Paragraph\",\n \"EmphasisMark\",\n \"Blockquote\",\n \"StrikethroughMark\",\n \"BulletList\",\n \"OrderedList\",\n \"ListItem\",\n \"LinkMark\",\n \"URL\",\n \"CodeMark\",\n \"CodeInfo\",\n \"CodeText\",\n \"HeaderMark\",\n \"TaskMarker\",\n]);\n\nfunction createDecorationsGetter() {\n let markdownDecorationsCache: Range<Decoration>[] = [];\n let markdownSelectionDecorationsCache: Range<Decoration>[] = [];\n\n function getDecorations(view: EditorView, isChanged: boolean, mouseReleased: boolean) {\n const processDecorations = isChanged;\n const processSelectionDecorations = isChanged || mouseReleased;\n const decorations: Range<Decoration>[] = processDecorations ? [] : markdownDecorationsCache;\n const selectionDecorations: Range<Decoration>[] = processSelectionDecorations\n ? []\n : markdownSelectionDecorationsCache;\n\n const contentEditable = view.contentDOM.getAttribute(\"contenteditable\");\n const isReadonly = !contentEditable || contentEditable === \"false\";\n\n for (const { from: fromVisible, to: toVisible } of view.visibleRanges) {\n syntaxTree(view.state).iterate({\n from: fromVisible,\n to: toVisible,\n enter: (node) => {\n if (SKIP_MARKS.has(node.name)) return;\n /** Decoration by change content */\n if (processDecorations)\n decorationFunctions.forEach((f) => f({ decorations, node, view }));\n\n /** Decoration by selection content */\n if (processSelectionDecorations)\n selectionDecorationFunctions.forEach((f) =>\n f({\n decorations: selectionDecorations,\n node,\n view,\n forceActive: isReadonly,\n }),\n );\n },\n });\n }\n\n if (processDecorations) {\n markdownDecorationsCache = decorations;\n }\n if (processSelectionDecorations) {\n markdownSelectionDecorationsCache = selectionDecorations;\n }\n\n return Decoration.set([...decorations, ...selectionDecorations], true);\n }\n\n return getDecorations;\n}\n\nexport const markdownDecorationPlugin = (stateConfig: MarkdownStateConfig) => {\n return ViewPlugin.fromClass(\n class DecorationMarkdown {\n decorations: DecorationSet;\n\n mouseReleased: boolean = true;\n\n dom: HTMLElement;\n\n view: EditorView;\n\n decorationGetter: (\n view: EditorView,\n isChanged: boolean,\n mouseReleased: boolean,\n ) => DecorationSet;\n\n constructor(view: EditorView) {\n this.decorationGetter = createDecorationsGetter();\n this.decorations = this.decorationGetter(view, true, this.mouseReleased);\n this.dom = view.dom;\n this.view = view;\n document.addEventListener(\"mousedown\", this.onMouseDown.bind(this));\n document.addEventListener(\"mouseup\", this.onMouseUp.bind(this));\n\n saveDispatch(() => {\n this.view.dispatch(\n this.view.state.update({\n effects: [imageSrcGetterEffect.of(stateConfig.imageSrcGetter)],\n }),\n );\n });\n }\n\n update(update: ViewUpdate) {\n const isDocumentChanged =\n update.docChanged ||\n update.viewportChanged ||\n syntaxTree(update.startState) != syntaxTree(update.state);\n this.decorations = this.decorationGetter(\n update.view,\n isDocumentChanged,\n this.mouseReleased,\n );\n }\n\n destroy() {\n document.removeEventListener(\"mousedown\", this.onMouseDown.bind(this));\n document.removeEventListener(\"mouseup\", this.onMouseUp.bind(this));\n }\n\n onMouseDown(this: DecorationMarkdown) {\n this.mouseReleased = false;\n }\n\n onMouseUp(this: DecorationMarkdown) {\n this.mouseReleased = true;\n if (this.view.state.selection.ranges[0].from !== this.view.state.selection.ranges[0].to) {\n saveDispatch(() => {\n this.view.dispatch(this.view.state.update({}));\n });\n }\n }\n },\n {\n decorations: (plugin) => plugin.decorations,\n },\n );\n};\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AA8BA,MAAM,iBAAiB,GAAuB;IAC5C,0BAA0B;IAC1B,oBAAoB;IACpB,oBAAoB;IACpB,sBAAsB;IACtB,0BAA0B;IAC1B,qBAAqB;IACrB,sBAAsB;IACtB,oBAAoB;IACpB,oBAAoB;IACpB,wBAAwB;IACxB,uBAAuB;IACvB,6BAA6B;IA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AA,IAAI,CAAC,aAAa,GAAG,IAAI;AACzB,YAAA,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE;gBACvF,YAAY,CAAC,MAAK;AAChB,oBAAA,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAChD,iBAAC,CAAC;;;KAGP,EACD;QACE,WAAW,EAAE,CAAC,MAAM,KAAK,MAAM,CAAC,WAAW;AAC5C,KAAA,CACF;AACH;;;;"}
|
|
1
|
+
{"version":3,"file":"markdown-decoration.js","sources":["../../../../src/extensions/markdown/markdown-decoration.ts"],"sourcesContent":["import { syntaxTree } from \"@codemirror/language\";\nimport { type Range } from \"@codemirror/state\";\nimport {\n Decoration,\n type DecorationSet,\n type EditorView,\n ViewPlugin,\n type ViewUpdate,\n} from \"@codemirror/view\";\nimport { saveDispatch } from \"@/lib/utils\";\nimport { blockquoteDecorationPlugin } from \"./blockquote\";\nimport { boldDecorationPlugin } from \"./bold\";\nimport { codeDecorationPlugin } from \"./code\";\nimport { headerDecorationPlugin } from \"./header\";\nimport { horizontalDecorationPlugin } from \"./horizontal\";\nimport { imageDecorationPlugin } from \"./image/image-decoration\";\nimport { italicDecorationPlugin } from \"./italic\";\nimport { autoLinkDecorationPlugin, linkDecorationPlugin } from \"./link\";\nimport { listDecorationPlugin } from \"./list\";\nimport type {\n DecorationPlugin,\n GetDecorationFunction,\n GetSelectionDecorationFunction,\n MarkdownDecorationSettings,\n} from \"./markdown-types\";\nimport { mentionDecorationPlugin } from \"./mention/mention-decoration\";\nimport { strikeThroughDecorationPlugin } from \"./strike-through\";\nimport { todoDecorationPlugin } from \"./todo\";\n\nconst decorationPlugins: DecorationPlugin[] = [\n blockquoteDecorationPlugin,\n boldDecorationPlugin,\n codeDecorationPlugin,\n headerDecorationPlugin,\n horizontalDecorationPlugin,\n imageDecorationPlugin,\n italicDecorationPlugin,\n linkDecorationPlugin,\n listDecorationPlugin,\n autoLinkDecorationPlugin,\n mentionDecorationPlugin,\n strikeThroughDecorationPlugin,\n todoDecorationPlugin,\n];\n\nlet decorationFunctions: GetDecorationFunction[] = [];\nlet selectionDecorationFunctions: GetSelectionDecorationFunction[] = [];\n\nfor (const plugin of decorationPlugins) {\n if (plugin.decorations) decorationFunctions = decorationFunctions.concat(plugin.decorations);\n if (plugin.selectionDecorations)\n selectionDecorationFunctions = selectionDecorationFunctions.concat(plugin.selectionDecorations);\n}\n\nconst SKIP_MARKS = new Set([\n \"Document\",\n \"Paragraph\",\n \"EmphasisMark\",\n \"Blockquote\",\n \"StrikethroughMark\",\n \"BulletList\",\n \"OrderedList\",\n \"ListItem\",\n \"LinkMark\",\n \"URL\",\n \"CodeMark\",\n \"CodeInfo\",\n \"CodeText\",\n \"HeaderMark\",\n \"TaskMarker\",\n]);\n\nfunction createDecorationsGetter(settings: MarkdownDecorationSettings) {\n let markdownDecorationsCache: Range<Decoration>[] = [];\n let markdownSelectionDecorationsCache: Range<Decoration>[] = [];\n\n function getDecorations(view: EditorView, isChanged: boolean, mouseReleased: boolean) {\n const processDecorations = isChanged;\n const processSelectionDecorations = isChanged || mouseReleased;\n const decorations: Range<Decoration>[] = processDecorations ? [] : markdownDecorationsCache;\n const selectionDecorations: Range<Decoration>[] = processSelectionDecorations\n ? []\n : markdownSelectionDecorationsCache;\n\n const contentEditable = view.contentDOM.getAttribute(\"contenteditable\");\n const isReadonly = !contentEditable || contentEditable === \"false\";\n\n for (const { from: fromVisible, to: toVisible } of view.visibleRanges) {\n syntaxTree(view.state).iterate({\n from: fromVisible,\n to: toVisible,\n enter: (node) => {\n if (SKIP_MARKS.has(node.name)) return;\n /** Decoration by change content */\n if (processDecorations)\n decorationFunctions.forEach((f) => f({ decorations, node, view, settings }));\n\n /** Decoration by selection content */\n if (processSelectionDecorations)\n selectionDecorationFunctions.forEach((f) =>\n f({\n decorations: selectionDecorations,\n node,\n view,\n forceActive: isReadonly,\n settings,\n }),\n );\n },\n });\n }\n\n if (processDecorations) {\n markdownDecorationsCache = decorations;\n }\n if (processSelectionDecorations) {\n markdownSelectionDecorationsCache = selectionDecorations;\n }\n\n return Decoration.set([...decorations, ...selectionDecorations], true);\n }\n\n return getDecorations;\n}\n\nexport const markdownDecorationPlugin = (settings: MarkdownDecorationSettings) => {\n return ViewPlugin.fromClass(\n class DecorationMarkdown {\n decorations: DecorationSet;\n\n mouseReleased: boolean = true;\n\n dom: HTMLElement;\n\n view: EditorView;\n\n decorationGetter: (\n view: EditorView,\n isChanged: boolean,\n mouseReleased: boolean,\n ) => DecorationSet;\n\n constructor(view: EditorView) {\n this.decorationGetter = createDecorationsGetter(settings);\n this.decorations = this.decorationGetter(view, true, this.mouseReleased);\n this.dom = view.dom;\n this.view = view;\n document.addEventListener(\"mousedown\", this.onMouseDown.bind(this));\n document.addEventListener(\"mouseup\", this.onMouseUp.bind(this));\n }\n\n update(update: ViewUpdate) {\n const isDocumentChanged =\n update.docChanged ||\n update.viewportChanged ||\n syntaxTree(update.startState) != syntaxTree(update.state);\n this.decorations = this.decorationGetter(\n update.view,\n isDocumentChanged,\n this.mouseReleased,\n );\n }\n\n destroy() {\n document.removeEventListener(\"mousedown\", this.onMouseDown.bind(this));\n document.removeEventListener(\"mouseup\", this.onMouseUp.bind(this));\n }\n\n onMouseDown(this: DecorationMarkdown) {\n this.mouseReleased = false;\n }\n\n onMouseUp(this: DecorationMarkdown) {\n this.mouseReleased = true;\n if (this.view.state.selection.ranges[0].from !== this.view.state.selection.ranges[0].to) {\n saveDispatch(() => {\n this.view.dispatch(this.view.state.update({}));\n });\n }\n }\n },\n {\n decorations: (plugin) => plugin.decorations,\n },\n );\n};\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AA6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YAAA,IAAI,CAAC,aAAa,GAAG,IAAI;AACzB,YAAA,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE;gBACvF,YAAY,CAAC,MAAK;AAChB,oBAAA,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAChD,iBAAC,CAAC;;;KAGP,EACD;QACE,WAAW,EAAE,CAAC,MAAM,KAAK,MAAM,CAAC,WAAW;AAC5C,KAAA,CACF;AACH;;;;"}
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { StateEffect, StateField } from '@codemirror/state';
|
|
2
|
+
import '@codemirror/view';
|
|
3
|
+
import { randomString } from '../../lib/utils/random-string.js';
|
|
2
4
|
|
|
3
5
|
const openedImageEffect = StateEffect.define();
|
|
4
6
|
const openedLinkEffect = StateEffect.define();
|
|
5
|
-
const imageSrcGetterEffect = StateEffect.define();
|
|
6
7
|
const markdownState = StateField.define({
|
|
7
8
|
create() {
|
|
8
9
|
return {
|
|
9
10
|
openedImage: undefined,
|
|
10
|
-
imageSrcGetter: undefined,
|
|
11
11
|
openedLink: undefined,
|
|
12
|
+
uniqueId: randomString(10),
|
|
12
13
|
};
|
|
13
14
|
},
|
|
14
15
|
update(value, transaction) {
|
|
@@ -16,8 +17,6 @@ const markdownState = StateField.define({
|
|
|
16
17
|
for (const effect of transaction.effects) {
|
|
17
18
|
if (effect.is(openedImageEffect))
|
|
18
19
|
newValue.openedImage = effect.value;
|
|
19
|
-
if (effect.is(imageSrcGetterEffect))
|
|
20
|
-
newValue.imageSrcGetter = effect.value;
|
|
21
20
|
if (effect.is(openedLinkEffect))
|
|
22
21
|
newValue.openedLink = effect.value;
|
|
23
22
|
}
|
|
@@ -25,5 +24,5 @@ const markdownState = StateField.define({
|
|
|
25
24
|
},
|
|
26
25
|
});
|
|
27
26
|
|
|
28
|
-
export {
|
|
27
|
+
export { markdownState, openedImageEffect, openedLinkEffect };
|
|
29
28
|
//# sourceMappingURL=markdown-state.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdown-state.js","sources":["../../../../src/extensions/markdown/markdown-state.ts"],"sourcesContent":["import { StateEffect, StateField } from \"@codemirror/state\";\nimport type { MarkdownState } from \"./markdown-types\";\n\nexport const openedImageEffect = StateEffect.define<string | undefined>();\nexport const openedLinkEffect = StateEffect.define<string | undefined>();\
|
|
1
|
+
{"version":3,"file":"markdown-state.js","sources":["../../../../src/extensions/markdown/markdown-state.ts"],"sourcesContent":["import { StateEffect, StateField } from \"@codemirror/state\";\nimport { randomString } from \"@/lib/utils\";\nimport type { MarkdownState } from \"./markdown-types\";\n\nexport const openedImageEffect = StateEffect.define<string | undefined>();\nexport const openedLinkEffect = StateEffect.define<string | undefined>();\n\nexport const markdownState = StateField.define<MarkdownState>({\n create() {\n return {\n openedImage: undefined,\n openedLink: undefined,\n uniqueId: randomString(10),\n };\n },\n\n update(value, transaction) {\n const newValue = { ...value };\n\n for (const effect of transaction.effects) {\n if (effect.is(openedImageEffect)) newValue.openedImage = effect.value;\n if (effect.is(openedLinkEffect)) newValue.openedLink = effect.value;\n }\n\n return newValue;\n },\n});\n"],"names":[],"mappings":";;;;MAIa,iBAAiB,GAAG,WAAW,CAAC,MAAM;MACtC,gBAAgB,GAAG,WAAW,CAAC,MAAM;AAErC,MAAA,aAAa,GAAG,UAAU,CAAC,MAAM,CAAgB;IAC5D,MAAM,GAAA;QACJ,OAAO;AACL,YAAA,WAAW,EAAE,SAAS;AACtB,YAAA,UAAU,EAAE,SAAS;AACrB,YAAA,QAAQ,EAAE,YAAY,CAAC,EAAE,CAAC;SAC3B;KACF;IAED,MAAM,CAAC,KAAK,EAAE,WAAW,EAAA;AACvB,QAAA,MAAM,QAAQ,GAAG,EAAE,GAAG,KAAK,EAAE;AAE7B,QAAA,KAAK,MAAM,MAAM,IAAI,WAAW,CAAC,OAAO,EAAE;AACxC,YAAA,IAAI,MAAM,CAAC,EAAE,CAAC,iBAAiB,CAAC;AAAE,gBAAA,QAAQ,CAAC,WAAW,GAAG,MAAM,CAAC,KAAK;AACrE,YAAA,IAAI,MAAM,CAAC,EAAE,CAAC,gBAAgB,CAAC;AAAE,gBAAA,QAAQ,CAAC,UAAU,GAAG,MAAM,CAAC,KAAK;;AAGrE,QAAA,OAAO,QAAQ;KAChB;AACF,CAAA;;;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
function randomString(length = 10) {
|
|
2
|
+
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
3
|
+
let result = "";
|
|
4
|
+
let counter = 0;
|
|
5
|
+
while (counter < length) {
|
|
6
|
+
result += characters.charAt(Math.floor(Math.random() * characters.length));
|
|
7
|
+
counter += 1;
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { randomString };
|
|
13
|
+
//# sourceMappingURL=random-string.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-string.js","sources":["../../../../src/lib/utils/random-string.ts"],"sourcesContent":["export function randomString(length: number = 10) {\n const characters = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\n let result = \"\";\n let counter = 0;\n\n while (counter < length) {\n result += characters.charAt(Math.floor(Math.random() * characters.length));\n counter += 1;\n }\n\n return result;\n}\n"],"names":[],"mappings":"AAAgB,SAAA,YAAY,CAAC,MAAA,GAAiB,EAAE,EAAA;IAC9C,MAAM,UAAU,GAAG,gEAAgE;IACnF,IAAI,MAAM,GAAG,EAAE;IACf,IAAI,OAAO,GAAG,CAAC;AAEf,IAAA,OAAO,OAAO,GAAG,MAAM,EAAE;AACvB,QAAA,MAAM,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;QAC1E,OAAO,IAAI,CAAC;;AAGd,IAAA,OAAO,MAAM;AACf;;;;"}
|
package/lib/index.d.ts
CHANGED
|
@@ -81,12 +81,14 @@ type GetDecorationOptions = {
|
|
|
81
81
|
node: SyntaxNodeRef;
|
|
82
82
|
decorations: Range<Decoration>[];
|
|
83
83
|
view: EditorView;
|
|
84
|
+
settings: MarkdownDecorationSettings;
|
|
84
85
|
};
|
|
85
86
|
type GetSelectionDecorationOptions = {
|
|
86
87
|
node: SyntaxNodeRef;
|
|
87
88
|
decorations: Range<Decoration>[];
|
|
88
89
|
view: EditorView;
|
|
89
90
|
forceActive: boolean;
|
|
91
|
+
settings: MarkdownDecorationSettings;
|
|
90
92
|
};
|
|
91
93
|
type GetDecorationFunction = (options: GetDecorationOptions) => void;
|
|
92
94
|
type GetSelectionDecorationFunction = (options: GetSelectionDecorationOptions) => void;
|
|
@@ -94,6 +96,9 @@ type DecorationPlugin = {
|
|
|
94
96
|
decorations?: GetDecorationFunction[];
|
|
95
97
|
selectionDecorations?: GetSelectionDecorationFunction[];
|
|
96
98
|
};
|
|
99
|
+
type MarkdownDecorationSettings = {
|
|
100
|
+
imageSrcGetter?: (src: string) => string;
|
|
101
|
+
};
|
|
97
102
|
|
|
98
103
|
type InitSettingsOptions = {
|
|
99
104
|
readonly?: boolean;
|