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 9b986a3152ed624e7fc0bbed7728278b02b48b4d
parent 580614e4c8b2b1db4017c493c008af7002525a2b
Author: unitehenry <unitehenry@gmail.com>
Date:   Wed, 14 Oct 2020 14:30:51 -0400

MERGE

Diffstat:
Ademo/.eslintignore | 1+
Mdemo/package-lock.json | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mdemo/package.json | 1+
Mdemo/src/test.js | 387++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Mpackage-lock.json | 2+-
Msrc/striveneditor.js | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
6 files changed, 440 insertions(+), 159 deletions(-)

diff --git a/demo/.eslintignore b/demo/.eslintignore @@ -0,0 +1 @@ +*.js diff --git a/demo/package-lock.json b/demo/package-lock.json @@ -939,6 +939,15 @@ } } }, + "@striven-erp/striven-editor": { + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/@striven-erp/striven-editor/-/striven-editor-3.8.3.tgz", + "integrity": "sha512-8df5ICMG9wyx9oAePmp+dTmOpgiQjxFFXTe/UH7eXR3JN6YeEjVzf49lMQGs5LXlDQa4im951+I1A8F99i7SRA==", + "requires": { + "js-beautify": "^1.11.0", + "linkifyjs": "^2.1.9" + } + }, "@types/events": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/@types/events/-/events-3.0.0.tgz", @@ -1739,6 +1748,11 @@ "integrity": "sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==", "dev": true }, + "abbrev": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", + "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==" + }, "accepts": { "version": "1.3.7", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", @@ -3055,6 +3069,15 @@ "typedarray": "^0.0.6" } }, + "config-chain": { + "version": "1.1.12", + "resolved": "https://registry.npmjs.org/config-chain/-/config-chain-1.1.12.tgz", + "integrity": "sha512-a1eOIcu8+7lUInge4Rpf/n4Krkf3Dd9lqhljRzII1/Zno/kRtUWnznPO3jOKBmTEktkt3fkxisUcivoj0ebzoA==", + "requires": { + "ini": "^1.3.4", + "proto-list": "~1.2.1" + } + }, "connect-history-api-fallback": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/connect-history-api-fallback/-/connect-history-api-fallback-1.6.0.tgz", @@ -4097,6 +4120,33 @@ "safer-buffer": "^2.1.0" } }, + "editorconfig": { + "version": "0.15.3", + "resolved": "https://registry.npmjs.org/editorconfig/-/editorconfig-0.15.3.tgz", + "integrity": "sha512-M9wIMFx96vq0R4F+gRpY3o2exzb8hEj/n9S8unZtHSvYjibBp/iMufSzvmOcV/laG0ZtuTVGtiJggPOSW2r93g==", + "requires": { + "commander": "^2.19.0", + "lru-cache": "^4.1.5", + "semver": "^5.6.0", + "sigmund": "^1.0.1" + }, + "dependencies": { + "lru-cache": { + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz", + "integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==", + "requires": { + "pseudomap": "^1.0.2", + "yallist": "^2.1.2" + } + }, + "yallist": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", + "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=" + } + } + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -6419,6 +6469,11 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, + "ini": { + "version": "1.3.5", + "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.5.tgz", + "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==" + }, "inquirer": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-3.3.0.tgz", @@ -6852,6 +6907,25 @@ } } }, + "js-beautify": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/js-beautify/-/js-beautify-1.13.0.tgz", + "integrity": "sha512-/Tbp1OVzZjbwzwJQFIlYLm9eWQ+3aYbBXLSaqb1mEJzhcQAfrqMMQYtjb6io+U6KpD0ID4F+Id3/xcjH3l/sqA==", + "requires": { + "config-chain": "^1.1.12", + "editorconfig": "^0.15.3", + "glob": "^7.1.3", + "mkdirp": "^1.0.4", + "nopt": "^5.0.0" + }, + "dependencies": { + "mkdirp": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", + "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==" + } + } + }, "js-levenshtein": { "version": "1.1.6", "resolved": "https://registry.npmjs.org/js-levenshtein/-/js-levenshtein-1.1.6.tgz", @@ -7022,6 +7096,11 @@ "integrity": "sha1-HADHQ7QzzQpOgHWPe2SldEDZ/wA=", "dev": true }, + "linkifyjs": { + "version": "2.1.9", + "resolved": "https://registry.npmjs.org/linkifyjs/-/linkifyjs-2.1.9.tgz", + "integrity": "sha512-74ivurkK6WHvHFozVaGtQWV38FzBwSTGNmJolEgFp7QgR2bl6ArUWlvT4GcHKbPe1z3nWYi+VUdDZk16zDOVug==" + }, "loader-fs-cache": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/loader-fs-cache/-/loader-fs-cache-1.0.2.tgz", @@ -7701,6 +7780,14 @@ "semver": "^5.3.0" } }, + "nopt": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz", + "integrity": "sha512-Tbj67rffqceeLpcRXrT7vKAN8CwfPeIBgM7E6iBkmKLV7bEMwpGgYLGv0jACUsECaa/vuxP0IjEont6umdMgtQ==", + "requires": { + "abbrev": "1" + } + }, "normalize-package-data": { "version": "2.5.0", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.5.0.tgz", @@ -8888,6 +8975,11 @@ "resolved": "https://registry.npmjs.org/promise-inflight/-/promise-inflight-1.0.1.tgz", "integrity": "sha1-mEcocL8igTL8vdhoEputEsPAKeM=" }, + "proto-list": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/proto-list/-/proto-list-1.2.4.tgz", + "integrity": "sha1-IS1b/hMYMGpCD2QCuOJv85ZHqEk=" + }, "proxy-addr": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.5.tgz", @@ -8907,8 +8999,7 @@ "pseudomap": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", - "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=", - "dev": true + "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=" }, "psl": { "version": "1.4.0", @@ -9544,8 +9635,7 @@ "semver": { "version": "5.7.1", "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", - "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", - "dev": true + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==" }, "send": { "version": "0.17.1", @@ -9745,6 +9835,11 @@ "integrity": "sha512-mRz/m/JVscCrkMyPqHc/bczi3OQHkLTqXHEFu0zDhK/qfv3UcOA4SVmRCLmos4bhjr9ekVQubj/R7waKapmiQg==", "dev": true }, + "sigmund": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/sigmund/-/sigmund-1.0.1.tgz", + "integrity": "sha1-P/IfGYytIXX587eBhT/ZTQ0ZtZA=" + }, "signal-exit": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", diff --git a/demo/package.json b/demo/package.json @@ -8,6 +8,7 @@ "lint": "vue-cli-service lint" }, "dependencies": { + "@striven-erp/striven-editor": "^3.8.3", "core-js": "^2.6.5", "terser-webpack-plugin": "^2.2.2", "tributejs": "^3.7.3", diff --git a/demo/src/test.js b/demo/src/test.js @@ -1,176 +1,244 @@ +import { denormalizeCamel } from '../../src/utils'; const { assert } = chai; -mocha.setup('bdd'); +mocha.setup("bdd"); function getEditor() { const editor = [...document.getElementsByClassName('striven-editor')].pop(); - return editor.StrivenEditor(); + return editor.StrivenEditor(); } function node(content) { - const node = document.createElement('div'); - node.innerHTML = content; - return node; + const node = document.createElement("div"); + node.innerHTML = content; + return node; } /* Regression Tests */ describe('Regression Tests', () => { - - it('should not return script elements', () => { - const se = getEditor(); - - const content = ` + it('should not return script elements', () => { + const se = getEditor(); + + const content = ` <p>This is some dangerous content!</p> <script>alert('hello');</script> `; - se.setContent(content); - const script = node(se.getContent()).querySelector('script'); + se.setContent(content); + const script = node(se.getContent()).querySelector('script'); - assert.notExists(script, 'script tags were pruned'); - se.clearContent(); + assert.notExists(script, 'script tags were pruned'); + se.clearContent(); + }); - }); + it('should create an ordered list with no content', () => { + const se = getEditor(); + se.toolbar.querySelector('#toolbar-insertOrderedList').click(); - it('should create an ordered list with no content', () => { - const se = getEditor(); - se.toolbar.querySelector('#toolbar-insertOrderedList').click(); + assert.exists(se.body.querySelector('ol'), 'an ordered list was created'); + se.clearContent(); + }); - assert.exists(se.body.querySelector('ol'), 'an ordered list was created'); - se.clearContent(); + it('should create an unordered list with no content', () => { + const se = getEditor(); + se.toolbar.querySelector('#toolbar-insertUnorderedList').click(); - }); + assert.exists(se.body.querySelector('ul'), 'an unordered list was created'); + se.clearContent(); + }); - it('should create an unordered list with no content', () => { - const se = getEditor(); - se.toolbar.querySelector('#toolbar-insertUnorderedList').click(); + it('should apply font style to content', done => { + const se = getEditor(); + se.setContent('<p>Change me to Verdana</p>'); - assert.exists(se.body.querySelector('ul'), 'an unordered list was created'); - se.clearContent(); + setTimeout(() => { + se.getRange().selectNode(se.body.querySelector('p')); + se.toolbar.querySelector('#toolbar-fontName').click(); + se.editor + .querySelector( + '.se-toolbar-popup-option[style="font-family: Verdana;"]' + ) + .click(); + se.body.onfocus(); + setTimeout(() => { + assert.exists( + se.body.querySelector('p font[face="Verdana"]'), + 'selected font style was applied' + ); + se.clearContent(); + done(); + }, 0); + }, 200); + }); - }); + it('should apply font size to content', done => { + const se = getEditor(); + se.setContent('<p>Change me to 36pt size</p>'); - it('should apply font style to content', (done) => { - const se = getEditor(); - se.setContent('<p>Change me to Verdana</p>'); - - setTimeout(() => { - se.getRange().selectNode(se.body.querySelector('p')); - se.toolbar.querySelector('#toolbar-fontName').click(); - se.editor.querySelector('.se-toolbar-popup-option[style="font-family: Verdana;"]').click(); - se.body.onfocus(); - setTimeout(() => { - assert.exists(se.body.querySelector('p font[face="Verdana"]'), 'selected font style was applied'); - se.clearContent(); - done(); - }, 0); - }, 200); - - }); - - it('should apply font size to content', (done) => { - const se = getEditor(); - se.setContent('<p>Change me to 36pt size</p>'); - - setTimeout(() => { - se.getRange().selectNode(se.body.querySelector('p')); - se.toolbar.querySelector('#toolbar-fontSize').click(); - const option = [...se.editor.querySelectorAll('.se-toolbar-popup-option')].filter(opt => (opt.textContent === '36pt')).pop(); - option.click(); - se.body.onfocus(); setTimeout(() => { - assert.exists(se.body.querySelector('font[size="7"]'), 'selected font size was applied'); - se.clearContent(); - done(); - }, 0); - }, 200); - - }); - - it('should apply font format to content', (done) => { - const se = getEditor(); - se.setContent('<p>Change me to an H1 format</p>'); - - setTimeout(() => { - se.getRange().selectNode(se.body.querySelector('p')); - se.toolbar.querySelector('#toolbar-fontFormat').click(); - const option = [...se.editor.querySelectorAll('.se-toolbar-popup-option')].filter(opt => (opt.textContent === 'Heading 1')).pop(); - option.click(); - se.body.onfocus(); - setTimeout(() => { - assert.exists(se.body.querySelector('h1'), 'selected font format was applied'); - se.clearContent(); - done(); - }, 0); - }, 200); - - }); - - it('should apply a scroll bar for large content', () => { - const se = getEditor(); - se.editor.setAttribute('max-height', '300px'); - se.body.style.maxHeight = '300px'; - - let largeContent = ''; - for(let i = 0; i < 1000; i++) { - largeContent += `<p>line ${i}</p>`; - } - - se.setContent(largeContent); - se.body.blur(); + se.getRange().selectNode(se.body.querySelector('p')); + se.toolbar.querySelector('#toolbar-fontSize').click(); + const option = [...se.editor.querySelectorAll('.se-toolbar-popup-option')] + .filter(opt => opt.textContent === '36pt') + .pop(); + option.click(); + se.body.onfocus(); + setTimeout(() => { + assert.exists( + se.body.querySelector('font[size="7"]'), + 'selected font size was applied' + ); + se.clearContent(); + done(); + }, 0); + }, 200); + }); - assert.notEqual(se.body.scrollHeight, se.body.offsetHeight, 'scroll bar was set'); - se.clearContent(); - se.overflow(); + it('should apply font format to content', done => { + const se = getEditor(); + se.setContent('<p>Change me to an H1 format</p>'); - }); - - it('should apply a scroll bar for large content after collapsing fullscreen', () => { - const se = getEditor(); - se.editor.setAttribute('max-height', '300px'); - se.body.style.maxHeight = '300px'; + setTimeout(() => { + se.getRange().selectNode(se.body.querySelector('p')); + se.toolbar.querySelector('#toolbar-fontFormat').click(); + const option = [...se.editor.querySelectorAll('.se-toolbar-popup-option')] + .filter(opt => opt.textContent === 'Heading 1') + .pop(); + option.click(); + se.body.onfocus(); + setTimeout(() => { + assert.exists( + se.body.querySelector('h1'), + 'selected font format was applied' + ); + se.clearContent(); + done(); + }, 0); + }, 200); + }); - se.toolbar.querySelector('#toolbar-fullscreen').click(); + it('should apply a scroll bar for large content', () => { + const se = getEditor(); + se.editor.setAttribute('max-height', '300px'); + se.body.style.maxHeight = '300px'; + + let largeContent = ''; + for (let i = 0; i < 1000; i++) { + largeContent += `<p>line ${i}</p>`; + } + + se.setContent(largeContent); + se.body.blur(); + + assert.notEqual( + se.body.scrollHeight, + se.body.offsetHeight, + 'scroll bar was set' + ); + se.clearContent(); + se.overflow(); + }); - let largeContent = ''; - for(let i = 0; i < 1000; i++) { - largeContent += `<p>line ${i}</p>`; - } + it('should apply a scroll bar for large content after collapsing fullscreen', () => { + const se = getEditor(); + se.editor.setAttribute('max-height', '300px'); + se.body.style.maxHeight = '300px'; - se.setContent(largeContent); - se.toolbar.querySelector('#toolbar-fullscreen').click(); - - assert.notEqual(se.body.scrollHeight, se.body.offsetHeight, 'scroll bar was set'); - se.clearContent(); - se.overflow(); + se.toolbar.querySelector('#toolbar-fullscreen').click(); - }); + let largeContent = ''; + for (let i = 0; i < 1000; i++) { + largeContent += `<p>line ${i}</p>`; + } - it('should attach a file', (done) => { + se.setContent(largeContent); + se.toolbar.querySelector('#toolbar-fullscreen').click(); - const se = getEditor(); + assert.notEqual( + se.body.scrollHeight, + se.body.offsetHeight, + 'scroll bar was set' + ); + se.clearContent(); + se.overflow(); + }); - fetch('https://raw.githubusercontent.com/striven-erp/striven-editor/master/LICENSE.txt') - .then(res => res.body.getReader().read().then(d => { - se.attachFile(new File([new Blob(d.value)], 'LICENSE.txt')); - - const isAttached = se.getFiles().pop() instanceof File; - const elCreated = se.filesSection.querySelector('.se-file .se-file-name').textContent === 'LICENSE.txt'; + it('should attach a file', done => { + const se = getEditor(); + + fetch( + 'https://raw.githubusercontent.com/striven-erp/striven-editor/master/LICENSE.txt' + ).then(res => + res.body + .getReader() + .read() + .then(d => { + se.attachFile(new File([new Blob(d.value)], 'LICENSE.txt')); + + const isAttached = se.getFiles().pop() instanceof File; + const elCreated = + se.filesSection.querySelector('.se-file .se-file-name') + .textContent === 'LICENSE.txt'; + + assert.isTrue(isAttached && elCreated, 'file was attached'); + se.clearFiles(); + done(); + }) + ); + }); - assert.isTrue(isAttached && elCreated, 'file was attached'); - se.clearFiles(); - done(); - - })); + it('should open/close link menu', done => { + const se = getEditor(); + var wasVisible = false; + se.openLinkMenu(); + assert.exists(document.querySelector('#link-menu'), 'menu is there'); + + se.openLinkMenu(); + setTimeout(function() { + if ( + window.getComputedStyle(document.getElementById('link-menu')) + .visibility == 'visible' + ) { + wasVisible = true; + se.closeLinkMenu(); + setTimeout(function() { + if ( + window.getComputedStyle(document.getElementById('link-menu')) + .visibility == 'hidden' + ) { + assert.isTrue(wasVisible); + done(); + } + }, 500); + } + }, 250); + }); - }); + it("should apply strikethrough decoration to content", (done) => { + const se = getEditor(); + se.setContent("<p>Strike Me Through</p>"); + setTimeout(() => { + se.getRange().selectNode(se.body.querySelector("p")); + se.body.blur(); + se.toolbar.querySelector("#toolbar-strikethrough").click(); + se.body.focus(); + setTimeout(() => { + assert.exists( + se.body.querySelector("strike"), + "strikethrough was applied" + ); + se.clearContent(); + done(); + }, 0); + }, 200); + }); }); /* Internal Tests */ describe('Internal Tests', () => { it('should of been created', () => { - assert.exists(getEditor().editor, 'Editor is created'); + assert.exists(getEditor().editor, 'Editor is created'); }); it('should have a body', () => { @@ -182,4 +250,63 @@ describe('Internal Tests', () => { }); }); +describe('Unit Tests', () => { + it('should denormalize camel case strings', () => { + assert.equal(denormalizeCamel('helloWorld'), 'Hello World'); + assert.equal(denormalizeCamel('aVariable'), 'A Variable'); + assert.equal( + denormalizeCamel('aVeryVeryVeryVeryVeryLongVariable'), + 'A Very Very Very Very Very Long Variable' + ); + assert.equal(denormalizeCamel('nocase'), 'Nocase'); + assert.equal(denormalizeCamel('has_Underscore'), 'Has_ Underscore'); + }); + + it('should passes bytes and returns a nicely formatted string of the size', () => { + const se = getEditor(); + const kb = 1024; + const mb = kb * kb; + const gb = Math.pow(kb, 3); + const tb = Math.pow(kb, 4); + const pb = Math.pow(kb, 5); + const eb = Math.pow(kb, 6); + const zb = Math.pow(kb, 7); + const yb = Math.pow(kb, 8); + + assert.equal(se.formatBytes(0), '0 Bytes'); + assert.equal(se.formatBytes(256), '256 Bytes'); + assert.equal(se.formatBytes(kb), '1 KB'); + assert.equal(se.formatBytes(mb), '1 MB'); + assert.equal(se.formatBytes(gb), '1 GB'); + assert.equal(se.formatBytes(tb), '1 TB'); + assert.equal(se.formatBytes(pb), '1 PB'); + assert.equal(se.formatBytes(eb), '1 EB'); + assert.equal(se.formatBytes(zb), '1 ZB'); + assert.equal(se.formatBytes(yb), '1 YB'); + }) + + it("should change position to static", () => { + const se = getEditor(); + const content = node( + `<div style="position: relative"></div> + <div style="position: fixed"></div> + <div style="position: sticky"></div> + <div style="position: absolute"></div>` + ); + + const pruned = se.pruneInlineStyles(content); + + const prunedElementsWithStyle = pruned.querySelectorAll('*[style]'); + const testArray = Array.from(prunedElementsWithStyle).filter(el=> el.style.position !== 'static') + assert.isEmpty(testArray, "inline styles are pruned"); + se.clearContent(); + }); + + it('should check if the input string is a valid URL', () => { + const se = getEditor(); + assert.isTrue(se.validURL('https://www.google.com'),'The string is a valid link'); + assert.isFalse(se.validURL('invalid.string'),'The string is an invalid link'); + }); +}); + mocha.run(); diff --git a/package-lock.json b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@striven-erp/striven-editor", - "version": "3.6.9", + "version": "3.8.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -18,7 +18,7 @@ import { import { createSVG, denormalizeCamel, - blowUpElement + blowUpElement } from './utils'; // Polyfills @@ -289,7 +289,7 @@ export default class StrivenEditor { // Focus back into the body se.body.focus(); } - break; + break; case 'removeFormat': se.executeCommand(command); @@ -1038,16 +1038,50 @@ export default class StrivenEditor { } } } - + + // Wrap pasted link content for resetting the range + let resolveLinkPaste; + const content = e.clipboardData.getData('text/html') || pastedText; + if(content) { + const pasteNode = document.createElement('span'); + pasteNode.innerHTML = content; + + if(pasteNode.querySelector('a') || se.validURL(content) || se.validURL(pasteNode.textContent)) { + e.preventDefault(); + + pasteNode.setAttribute('class', 'se-pasted-content'); + se.executeCommand('insertHTML', pasteNode.outerHTML); + + resolveLinkPaste = () => { + // Collapse on pasted content + const pasteContent = se.body.querySelector('.se-pasted-content'); + const range = se.getRange(); + + if(range && pasteContent) { + range.selectNode(pasteContent); + range.collapse(); + + se.convertLinks(false, pasteContent); + + const resContent = () => { + pasteContent.outerHTML = pasteContent.innerHTML; + se.body.removeEventListener('blur', resContent); + }; + + se.body.addEventListener('blur', resContent); + } + } + } + } // After the paste setTimeout(() => { // Prune inline styles se.pruneInlineStyles(se.body); - // Convert all the links - se.convertAndSelectLinks(); + // Collapse on pasted content containing links + resolveLinkPaste && resolveLinkPaste(); // Editor After Paste Handler se.options.afterPaste && se.options.afterPaste(e); @@ -1081,7 +1115,7 @@ export default class StrivenEditor { break; case 'Enter': case ' ': - se.validURL(se.textBuffer) && se.convertAndSelectLinks(); + se.validURL(se.textBuffer) && se.convertLinks(true); se.textBuffer = null; break; case 'Semicolon': @@ -1151,8 +1185,24 @@ export default class StrivenEditor { !se.browser.isEdge() && se.setRange(); window.addEventListener('mouseup', execRange); - + se.editor.classList.add('se-focus'); + + if(se.body.textContent.trim() === '') { + const r = se.getRange(); + + if(r) { + const selNode = document.createTextNode(''); + se.body.append(selNode); + + setTimeout(() => { + se.getRange().selectNode(selNode); + selNode.remove(); + }, 0); + + } + + } if (se.scrollPosition && !se.browser.isEdge()) { body.scrollTo(se.scrollPosition); @@ -1201,7 +1251,7 @@ export default class StrivenEditor { }; se.textBuffer = null; - se.convertAndSelectLinks(false); + se.convertLinks(); bodyBlur && bodyBlur(); }; @@ -2165,7 +2215,7 @@ export default class StrivenEditor { /** * Prunes inline position styles from elements * @param {HTMLElement} el Element to prune inline styles from - * @returns {HTMLElement} Reutrns the sanitized element + * @returns {HTMLElement} Returns the sanitized element */ pruneInlineStyles(el) { let inlineStyleNodes = [...el.querySelectorAll('[style]')]; @@ -2781,15 +2831,22 @@ export default class StrivenEditor { } /** - * Convert and select links - * @param {Boolean} Select the last link + * Convert links + * @param {Boolean} Select the last converted link + * @parma {HTMLElement} Element to convert links in */ - convertAndSelectLinks(sel = true) { + convertLinks(selectLast, el) { const se = this; - linkify(se.body, {}, document); - + + linkify(el || se.body, {}, document); const linkElements = se.body.getElementsByTagName('a'); + let lastConverted; + + if(selectLast) { + lastConverted = se.body.querySelector('.linkified'); + } + if (linkElements.length > 0) { const links = [...linkElements]; const convertedLinks = []; @@ -2830,12 +2887,11 @@ export default class StrivenEditor { link.classList.remove('linkified'); link.setAttribute('target', '_blank'); }); + } - if (sel) { - const range = se.getRange(); - range.selectNode(convertedLinks.pop()); - range.collapse(); - } + if(lastConverted && se.range) { + se.range.selectNode(lastConverted); + se.range.collapse(); } setTimeout(() => { @@ -2895,7 +2951,8 @@ export default class StrivenEditor { se.setRange(r); const b = document.createElement(el); - const selNode = document.createElement('span'); + const selNode = se.body.querySelector('.se-init-sel') || document.createElement('span'); + selNode.setAttribute('class', 'se-init-sel'); selNode.innerHTML = '&nbsp;'; b.append(selNode); @@ -3162,22 +3219,22 @@ export default class StrivenEditor { } break; case 'bold': - if(se.body.textContent === '') { + if(se.body.textContent.trim() === '') { textDecorationInsert('b'); break; } case 'italic': - if(se.body.textContent === '') { + if(se.body.textContent.trim() === '') { textDecorationInsert('i'); break; } case 'underline': - if(se.body.textContent === '') { + if(se.body.textContent.trim() === '') { textDecorationInsert('u'); break; } case 'strikethrough': - if(se.body.textContent === '') { + if(se.body.textContent.trim() === '') { textDecorationInsert('strike'); break; }