commit 920f5d8de74d0e13055586bc7013a1b4fae4d28b
parent bd9ccac756d30b4f3ed2363b5786fbbe09296bfd
Author: unitehenry <unitehenry@gmail.com>
Date: Fri, 22 Nov 2019 09:53:35 -0500
set inserted and pasted link target=blank
Diffstat:
2 files changed, 19 insertions(+), 9 deletions(-)
diff --git a/src/striveneditor.js b/src/striveneditor.js
@@ -459,6 +459,9 @@ export default class StrivenEditor {
document.execCommand('insertHTML', true, `<a href="${string}">${string}</a>`);
}
+ const links = [...this.body.querySelectorAll(`a[href="${string}"]`)];
+ links.forEach(link => link.setAttribute('target', '_blank'));
+
// get meta data
if (this.options.metaUrl) {
this.getMeta(string).then(res => {
@@ -472,7 +475,6 @@ export default class StrivenEditor {
}
}
- this.setLinks();
this.overflow();
};
@@ -526,7 +528,6 @@ export default class StrivenEditor {
const bodyBlur = body.onblur;
body.onblur = (e) => {
this.editor.classList.remove("se-focus");
- this.setLinks();
bodyBlur && bodyBlur();
}
@@ -540,7 +541,6 @@ export default class StrivenEditor {
const bodyKeyPress = body.onkeypress;
body.onkeypress = () => {
bodyKeyPress && bodyKeyPress();
- this.setLinks();
}
return body;
@@ -590,9 +590,12 @@ export default class StrivenEditor {
document.execCommand("createLink", false, linkValue)
}
else {
- document.execCommand("createLink", true, linkValue)
+ document.execCommand("createLink", true, linkValue);
}
+ const links = [...this.body.querySelectorAll(`a[href="${linkValue}"]`)];
+ links.forEach(link => link.setAttribute('target', '_blank'));
+
if (this.options.metaUrl && this.validURL(linkValue)) {
this.getMeta(linkValue).then(res => {
const { url, image, title, description } = res;
@@ -1064,6 +1067,8 @@ export default class StrivenEditor {
}
getContent() {
+ // this.setLinkTarget();
+
const cleanBody = this.pruneScripts(this.body);
const text = cleanBody.textContent;
@@ -1380,9 +1385,15 @@ export default class StrivenEditor {
return isEditor(activeEl);
}
- setLinks() {
+ setLinkTarget() {
const links = [...this.body.querySelectorAll('a')];
- links.length && links.forEach(link => link.setAttribute('target', 'blank'))
+ if (links.length) {
+ links.forEach(link => {
+ if (!link.onclick || link.href) {
+ link.setAttribute('target', '_blank');
+ }
+ })
+ }
}
executeCommand(command) {
@@ -1430,7 +1441,7 @@ export default class StrivenEditor {
} else {
this.openLinkMenu();
- this.linkMenu.querySelector('input').focus();
+ setTimeout(() => this.linkMenu.querySelector('input').focus(), 100)
}
break;
case "image":
@@ -1440,7 +1451,7 @@ export default class StrivenEditor {
this.openImageMenu();
this.range = this.getRange();
- this.imageMenu.querySelector('input').focus();
+ setTimeout(() => this.imageMenu.querySelector('input').focus(), 100)
}
break;
default:
diff --git a/src/vue-striveneditor.vue b/src/vue-striveneditor.vue
@@ -35,7 +35,6 @@ export default {
// Listen to input event for v-model support
vm.editor.body.oninput = e => {
vm.pauseUpdate = true;
- vm.editor.setLinks();
vm.$emit("input", vm.editor.getContent());
};
},