| 1 |
import { closestElement } from "@html_editor/utils/dom_traversal"; |
| 2 |
import { URL_REGEX, cleanZWChars } from "./utils"; |
| 3 |
import { isImageUrl } from "@html_editor/utils/url"; |
| 4 |
import { Plugin } from "@html_editor/plugin"; |
| 5 |
import { childNodeIndex } from "@html_editor/utils/position"; |
| 6 |
|
| 7 |
/** |
| 8 |
* @typedef {((text: string, url: string) => void | true)[]} paste_url_overrides |
| 9 |
*/ |
| 10 |
|
| 11 |
export class LinkPastePlugin extends Plugin { |
| 12 |
static id = "linkPaste"; |
| 13 |
static dependencies = ["link", "clipboard", "selection", "dom", "history"]; |
| 14 |
/** @type {import("plugins").EditorResources} */ |
| 15 |
resources = { |
| 16 |
before_paste_handlers: this.selectFullySelectedLink.bind(this), |
| 17 |
paste_text_overrides: this.handlePasteText.bind(this), |
| 18 |
}; |
| 19 |
|
| 20 |
/** |
| 21 |
* @param {EditorSelection} selection |
| 22 |
* @param {string} text |
| 23 |
*/ |
| 24 |
handlePasteText(selection, text) { |
| 25 |
let splitAroundUrl; |
| 26 |
// todo: add placeholder plugin that prevent any other plugin |
| 27 |
// Avoid transforming dynamic placeholder pattern to url. |
| 28 |
if (!text.match(/\${.*}/gi)) { |
| 29 |
splitAroundUrl = text.split(URL_REGEX); |
| 30 |
// Remove 'http(s)://' capturing group from the result (indexes |
| 31 |
// 2, 5, 8, ...). |
| 32 |
splitAroundUrl = splitAroundUrl.filter((_, index) => (index + 1) % 3); |
| 33 |
} |
| 34 |
if ( |
| 35 |
!splitAroundUrl || |
| 36 |
splitAroundUrl.length < 3 || |
| 37 |
closestElement(selection.anchorNode, "pre") |
| 38 |
) { |
| 39 |
// Let the default paste handle the text. |
| 40 |
return false; |
| 41 |
} |
| 42 |
if (splitAroundUrl.length === 3 && !splitAroundUrl[0] && !splitAroundUrl[2]) { |
| 43 |
// Pasted content is a single URL. |
| 44 |
this.handlePasteTextUrl(selection, text); |
| 45 |
} else { |
| 46 |
this.handlePasteTextMultiUrl(selection, splitAroundUrl); |
| 47 |
} |
| 48 |
return true; |
| 49 |
} |
| 50 |
/** |
| 51 |
* @param {EditorSelection} selection |
| 52 |
* @param {string} text |
| 53 |
*/ |
| 54 |
handlePasteTextUrl(selection, text) { |
| 55 |
const selectionIsInsideALink = !!closestElement(selection.anchorNode, "a"); |
| 56 |
const url = /^https?:\/\//i.test(text) ? text : "http://" + text; |
| 57 |
if (selectionIsInsideALink) { |
| 58 |
this.handlePasteTextUrlInsideLink(text, url); |
| 59 |
return; |
| 60 |
} |
| 61 |
if (this.delegateTo("paste_url_overrides", text, url)) { |
| 62 |
return; |
| 63 |
} |
| 64 |
this.dependencies.link.insertLink(url, text); |
| 65 |
} |
| 66 |
/** |
| 67 |
* @param {string} text |
| 68 |
* @param {string} url |
| 69 |
*/ |
| 70 |
handlePasteTextUrlInsideLink(text, url) { |
| 71 |
// A url cannot be transformed inside an existing link. |
| 72 |
// An image can be embedded inside an existing link, a video cannot. |
| 73 |
if (isImageUrl(url)) { |
| 74 |
const img = this.document.createElement("IMG"); |
| 75 |
img.setAttribute("src", url); |
| 76 |
this.dependencies.dom.insert(img); |
| 77 |
} else { |
| 78 |
this.dependencies.dom.insert(text); |
| 79 |
} |
| 80 |
} |
| 81 |
/** |
| 82 |
* @param {EditorSelection} selection |
| 83 |
* @param {string[]} splitAroundUrl |
| 84 |
*/ |
| 85 |
handlePasteTextMultiUrl(selection, splitAroundUrl) { |
| 86 |
const selectionIsInsideALink = !!closestElement(selection.anchorNode, "a"); |
| 87 |
for (let i = 0; i < splitAroundUrl.length; i++) { |
| 88 |
const url = /^https?:\/\//gi.test(splitAroundUrl[i]) |
| 89 |
? splitAroundUrl[i] |
| 90 |
: "http://" + splitAroundUrl[i]; |
| 91 |
// Even indexes will always be plain text, and odd indexes will always be URL. |
| 92 |
// A url cannot be transformed inside an existing link. |
| 93 |
if (i % 2 && !selectionIsInsideALink) { |
| 94 |
this.dependencies.dom.insert( |
| 95 |
this.dependencies.link.createLink(url, splitAroundUrl[i]) |
| 96 |
); |
| 97 |
} else if (splitAroundUrl[i] !== "") { |
| 98 |
this.dependencies.clipboard.pasteText(splitAroundUrl[i]); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @param {EditorSelection} selection |
| 105 |
*/ |
| 106 |
selectFullySelectedLink(selection) { |
| 107 |
const link = closestElement(selection.anchorNode, "a"); |
| 108 |
if ( |
| 109 |
link?.parentElement?.isContentEditable && |
| 110 |
cleanZWChars(selection.textContent()) === cleanZWChars(link.innerText) && |
| 111 |
!this.getResource("unremovable_node_predicates").some((p) => p(link)) |
| 112 |
) { |
| 113 |
this.dependencies.selection.setSelection({ |
| 114 |
anchorNode: link.parentElement, |
| 115 |
anchorOffset: childNodeIndex(link) + (selection.direction ? 0 : 1), |
| 116 |
focusNode: link.parentElement, |
| 117 |
focusOffset: childNodeIndex(link) + (selection.direction ? 1 : 0), |
| 118 |
}); |
| 119 |
} |
| 120 |
} |
| 121 |
} |