Skip to content

Commit c7259b0

Browse files
committed
[IMP] add an options to add html parameters in generated links
1 parent e06639c commit c7259b0

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/commands.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ export const editorCommands = {
325325
if (res) {
326326
setCursor(sel.anchorNode, sel.anchorOffset, sel.focusNode, sel.focusOffset);
327327
const node = findNode(closestPath(sel.focusNode), node => node.tagName === 'A');
328+
for (const [param, value] of Object.entries(editor.options.defaultLinkAttributes)) {
329+
node.setAttribute(param, `${value}`);
330+
}
328331
const pos = [node.parentElement, childNodeIndex(node) + 1];
329332
setCursor(...pos, ...pos, false);
330333
}

src/editor.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export class OdooEditor extends EventTarget {
7878
},
7979
toSanitize: true,
8080
isRootEditable: true,
81+
defaultLinkAttributes: {},
8182
getContentEditableAreas: () => [],
8283
_t: string => string,
8384
},
@@ -1767,7 +1768,12 @@ export class OdooEditor extends EventTarget {
17671768
_createLinkWithUrlInTextNode(textNode, url, index, length) {
17681769
setCursor(textNode, index, textNode, index + length);
17691770
this.document.execCommand('createLink', false, url);
1770-
this.document.getSelection().collapseToEnd();
1771+
const sel = this.document.getSelection();
1772+
const link = closestElement(sel.anchorNode, 'a');
1773+
for (const [param, value] of Object.entries(this.options.defaultLinkAttributes)) {
1774+
link.setAttribute(param, `${value}`);
1775+
}
1776+
sel.collapseToEnd();
17711777
}
17721778

17731779
/**
@@ -1777,6 +1783,11 @@ export class OdooEditor extends EventTarget {
17771783
ev.preventDefault();
17781784
const pastedText = (ev.originalEvent || ev).clipboardData.getData('text/plain');
17791785
const splitAroundUrl = pastedText.split(URL_REGEX);
1786+
const linkAttrs =
1787+
Object.entries(this.options.defaultLinkAttributes)
1788+
.map(entry => entry.join('="'))
1789+
.join('" ') + '" ';
1790+
17801791
for (let i = 0; i < splitAroundUrl.length; i++) {
17811792
// Even indexes will always be plain text, and odd indexes will always be URL.
17821793
if (i % 2) {
@@ -1785,7 +1796,7 @@ export class OdooEditor extends EventTarget {
17851796
: 'https://' + splitAroundUrl[i];
17861797
this.execCommand(
17871798
'insertHTML',
1788-
'<a href="' + url + '" >' + splitAroundUrl[i] + '</a>',
1799+
`<a href="${url}" ${linkAttrs}>${splitAroundUrl[i]}</a>`,
17891800
);
17901801
} else if (splitAroundUrl[i] !== '') {
17911802
this.execCommand('insertText', splitAroundUrl[i]);

src/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function startEditor(testHTML) {
88
const editor = new OdooEditor(editableContainer, {
99
toolbar: document.querySelector('#toolbar'),
1010
autohideToolbar: true,
11+
defaultLinkAttributes: { target: '_blank', rel: 'ugc' },
1112
});
1213
editor.historyFetch();
1314

0 commit comments

Comments
 (0)