striven-editor

Maintained and contributed to an open source WYSIWYG editor as an internship project that made it to production and was adopted by the company I was working at.
git clone https://unitehenry.com/git/striven-editor.git
Log | Files | Refs | README | LICENSE

commit 2875cd412dd87b39887f7385995ad90d630a7d1d
parent b268274a3cd606537700a92684c9d8362473771b
Author: Henry Unite <unitehenry@gmail.com>
Date:   Tue, 20 Oct 2020 16:29:12 -0400

Merge pull request #148 from unitehenry/dev

Link Editing
Diffstat:
Msrc/striveneditor.css | 28++++++++++++++++++++++++++++
Msrc/striveneditor.js | 191++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
2 files changed, 179 insertions(+), 40 deletions(-)

diff --git a/src/striveneditor.css b/src/striveneditor.css @@ -38,6 +38,34 @@ width: max-content; } +.striven-editor .se-body a { + position: relative; +} + +.striven-editor .se-body .se-link-options { + position: absolute; + left: 0; + top: 100%; + padding: 2px 5px; + background-color: #fff; + min-width: max-content; + border: 1px solid #ddd; + box-shadow: 5px 5px 5px #ddd; + font-size: 14px; +} + +.striven-editor .se-body .se-link-options span { + cursor: pointer; + color: #428bca; + text-decoration: underline; +} + +.striven-editor .se-body .se-link-options span:first-child:after { + content: '|'; + margin: 0 5px; + color: #000; +} + .striven-editor .se-body .se-file-drop-dropzone { height: 90%; width: 95%; diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -203,6 +203,14 @@ export default class StrivenEditor { } }); + // Remove all link option popups on escape + window.addEventListener('keydown', (e) => { + if(e.key === 'Escape') { + [...se.body.querySelectorAll('.se-link-options')] + .forEach(o => o.remove()); + } + }); + // Toolbar Hide if (se.options.toolbarHide) { // Hide the toolbar template if there is one @@ -603,24 +611,27 @@ export default class StrivenEditor { fontOption.textContent = f; fontOption.style.fontFamily = f; - fontOption.onclick = e => { + const trigger = e => { let fontselect = e.target.textContent; select.textContent = fontselect; menu.close(); - - const refocus = se.body.onfocus; - se.body.onfocus = () => { - function execute() { + + function execute() { if (fontselect === '(inherited font)') { fontselect = getComputedStyle(se.body).fontFamily; } - + if (se.browser.isEdge() || se.browser.isFirefox()) { document.execCommand('fontName', false, fontselect); } else { document.execCommand('fontName', true, fontselect); } - } + } + + execute(); + + const refocus = se.body.onfocus; + se.body.onfocus = () => { se.setRange(se.range); @@ -645,10 +656,15 @@ export default class StrivenEditor { se.body.focus(); }; - + + fontOption.onmousedown = trigger; + fontOption.onclick = trigger; + menu.append(fontOption); }); + + return menu; } @@ -664,9 +680,8 @@ export default class StrivenEditor { fontOption.classList.add('se-toolbar-popup-option'); fontOption.textContent = s; - - fontOption.onclick = e => { - function execute() { + + function execute() { let execSize = size; if (size === '(inherited size)') { @@ -678,15 +693,18 @@ export default class StrivenEditor { } else { document.execCommand('fontSize', true, execSize); } - } + } + const trigger = e => { const fontsize = e.target.textContent; select.textContent = fontsize; select.dataset.command = size; - + menu.close(); + execute(); + const refocus = se.body.focus; se.body.onfocus = () => { se.setRange(se.range); @@ -713,6 +731,9 @@ export default class StrivenEditor { se.body.focus(); }; + fontOption.onmousedown = trigger; + fontOption.onclick = trigger; + menu.append(fontOption); }); @@ -1067,8 +1088,12 @@ export default class StrivenEditor { se.convertLinks(false, pasteContent); const resContent = () => { - pasteContent.outerHTML = pasteContent.innerHTML; - se.body.removeEventListener('blur', resContent); + try { + pasteContent.outerHTML = pasteContent.innerHTML; + se.body.removeEventListener('blur', resContent); + } catch(e) { + se.body.removeEventListener('blur', resContent); + } }; se.body.addEventListener('blur', resContent); @@ -1328,25 +1353,33 @@ export default class StrivenEditor { se.setRange(); if (linkValue) { - if (se.browser.isFirefox() || se.browser.isEdge()) { - document.execCommand('createLink', false, linkValue); + const linkToEdit = se.body.querySelector('.se-link-to-edit'); + if(linkToEdit) { + linkToEdit.setAttribute('href', linkValue); + linkToEdit.innerText = textRowInput.value; + linkToEdit.classList.remove('se-link-to-edit'); + linkToEdit.setAttribute('contenteditable', true); } else { - document.execCommand('createLink', true, linkValue); - } - - const insertedLink = se.getRange().commonAncestorContainer - .parentElement; - if (insertedLink) { - windowRowInput.checked && - insertedLink.setAttribute('target', '_blank'); - windowRowInput.checked = true; - - if (textRowInput.value) { - insertedLink.textContent = textRowInput.value; - textRowInput.value = ''; + if (se.browser.isFirefox() || se.browser.isEdge()) { + document.execCommand('createLink', false, linkValue); + } else { + document.execCommand('createLink', true, linkValue); + } + + const insertedLink = se.getRange().commonAncestorContainer + .parentElement; + if (insertedLink) { + windowRowInput.checked && + insertedLink.setAttribute('target', '_blank'); + windowRowInput.checked = true; + + if (textRowInput.value) { + insertedLink.textContent = textRowInput.value; + textRowInput.value = ''; + } + + setTimeout(() => se.convertLinks(false, insertedLink), 10); } - - insertedLink.parsed = true; } if (se.options.metaUrl && se.validURL(linkValue)) { @@ -1393,6 +1426,7 @@ export default class StrivenEditor { }; linkMenuCloseButton.onclick = e => { + linkMenu.targetLink = null; se.body.focus(); se.closeLinkMenu(); resetInput(); @@ -2104,7 +2138,7 @@ export default class StrivenEditor { const se = this; const body = se.pruneScripts(se.body); const text = body.textContent; - + if (text || se.body.getElementsByTagName('img').length) { const htmlView = !!se.editor.querySelector('.se-html'); return htmlView ? text : body.innerHTML; @@ -2497,9 +2531,7 @@ export default class StrivenEditor { se.clearContent(); if (html) { se.body.innerHTML = html; - [...se.body.querySelectorAll('a')].forEach( - link => (link.parsed = true), - ); + se.convertLinks(); } } @@ -2853,9 +2885,6 @@ export default class StrivenEditor { const links = [...linkElements]; const convertedLinks = []; links.forEach(link => { - if (link.parsed) { - return false; - } const href = link.getAttribute('href'); const isEmail = href ? href.includes('mailto') : false; @@ -2866,6 +2895,7 @@ export default class StrivenEditor { if (href && (isEmail || isLinkified)) { link.outerHTML = link.textContent; } else { + link.onmousemove = e => { e.ctrlKey && (link.style.cursor = 'pointer'); }; @@ -2878,6 +2908,64 @@ export default class StrivenEditor { document.body.append(anchor); anchor.click(); anchor.remove(); + } else { + if(!link.querySelector('.se-link-options')) { + const linkOptions = document.createElement('span'); + linkOptions.setAttribute('class', 'se-link-options'); + linkOptions.setAttribute('contenteditable', false); + linkOptions.onclick = e => e.stopPropagation(); + link.append(linkOptions); + + const removeOption = document.createElement('span'); + removeOption.textContent = 'Remove'; + removeOption.onclick = (e) => { + e.preventDefault(); + e.stopPropagation(); + linkOptions.remove(); + + const replNode = document.createElement('span'); + replNode.setAttribute('class', 'se-link-ignore'); + replNode.textContent = link.textContent; + link.replaceWith(replNode); + + } + + linkOptions.append(removeOption); + + const changeOption = document.createElement('span'); + changeOption.textContent = 'Change'; + changeOption.onclick = (e) => { + e.preventDefault(); + e.stopPropagation(); + linkOptions.remove(); + + link.classList.add('se-link-to-edit'); + se.openLinkMenu(); + + const linkInputs = se.linkMenu.querySelectorAll('input'); + linkInputs[0]['value'] = link.getAttribute('href'); + linkInputs[1]['value'] = link.textContent; + } + + linkOptions.append(changeOption); + + const optionHandler = () => { + try { + const r = se.getRange(); + const rNode = r['commonAncestorContainer']['parentElement']; + if(rNode !== link) { + linkOptions.remove(); + se.body.removeEventListener('click', optionHandler); + } + } catch(e) { + linkOptions.remove(); + window.removeEventListener('click', optionHandler); + } + } + + window.addEventListener('click', optionHandler); + + } } }; @@ -2885,8 +2973,9 @@ export default class StrivenEditor { convertedLinks.push(link); } - + link.classList.remove('linkified'); + link.setAttribute('contenteditable', true); link.setAttribute('target', '_blank'); }); } @@ -2896,6 +2985,9 @@ export default class StrivenEditor { se.range.collapse(); } + [...se.body.querySelectorAll('.se-link-ignore a')] + .forEach(ign => ign.outerHTML = ign.textContent); + setTimeout(() => { // Trigger oninput event if (se.body.oninput) { @@ -3188,6 +3280,25 @@ export default class StrivenEditor { if (se.linkMenu.dataset.active === 'true') { se.closeLinkMenu(); } else { + const r = se.getRange(); + const rnode = document.createElement('div'); + rnode.append(r.cloneContents().cloneNode(true)); + + if(rnode.querySelector('br')){ + return; + } + + const rLink = rnode.querySelector('a'); + if(rLink) { + const linkRepl = document.createElement('a'); + linkRepl.textContent = rnode.textContent; + linkRepl.setAttribute('href', rLink.getAttribute('href')); + r.deleteContents(); + r.insertNode(linkRepl); + se.convertLinks(false, linkRepl); + return; + } + se.openLinkMenu(); setTimeout(() => {