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 1770ccf9cf31c506e8007c03a5ced9f2a44dbd4f
parent 207f934cc7896e585c5e5fa1acb707b2ebd27367
Author: Henry Unite <unitehenry@gmail.com>
Date:   Wed, 17 Jun 2020 14:08:47 -0400

latest editor

Diffstat:
Mpackage-lock.json | 140+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
Mpackage.json | 5+++--
Asrc/blowupelement.js | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/createSVG.js | 20++++++++++++++++++++
Msrc/defaults.js | 34++++++++++++++++++++++++++++++++--
Msrc/striveneditor.css | 107++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
Msrc/striveneditor.js | 744+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
7 files changed, 893 insertions(+), 206 deletions(-)

diff --git a/package-lock.json b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@striven-erp/striven-editor", - "version": "3.1.0", + "version": "3.4.93", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1148,6 +1148,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", @@ -1392,8 +1397,7 @@ "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", - "dev": true + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, "base": { "version": "0.11.2", @@ -1508,7 +1512,6 @@ "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -1991,8 +1994,7 @@ "commander": { "version": "2.20.0", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.0.tgz", - "integrity": "sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==", - "dev": true + "integrity": "sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==" }, "commondir": { "version": "1.0.1", @@ -2006,16 +2008,10 @@ "integrity": "sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg==", "dev": true }, - "computed-style-to-inline-style": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/computed-style-to-inline-style/-/computed-style-to-inline-style-3.0.0.tgz", - "integrity": "sha1-klECRGPFVcfhdo13oZ66m49Bivw=" - }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, "concat-stream": { "version": "1.6.2", @@ -2035,6 +2031,15 @@ "integrity": "sha512-Ath9o58/0rxZXbyoy3zZgrVMoIemi30sukG/btuMKCLyqfQt3dNOWc9N3EHEMa2Q3i0tXQPDJluYFLwy7pJuQw==", "dev": true }, + "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" + } + }, "console-browserify": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/console-browserify/-/console-browserify-1.1.0.tgz", @@ -2495,6 +2500,33 @@ "stream-shift": "^1.0.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", @@ -3032,8 +3064,7 @@ "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", - "dev": true + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, "fsevents": { "version": "1.2.9", @@ -3630,7 +3661,6 @@ "version": "7.1.4", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.4.tgz", "integrity": "sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A==", - "dev": true, "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -4014,7 +4044,6 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", - "dev": true, "requires": { "once": "^1.3.0", "wrappy": "1" @@ -4023,14 +4052,12 @@ "inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true + "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==", - "dev": true + "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==" }, "interpret": { "version": "1.2.0", @@ -4284,6 +4311,25 @@ "supports-color": "^6.1.0" } }, + "js-beautify": { + "version": "1.11.0", + "resolved": "https://registry.npmjs.org/js-beautify/-/js-beautify-1.11.0.tgz", + "integrity": "sha512-a26B+Cx7USQGSWnz9YxgJNMmML/QG2nqIaL7VVYPCXbqiKz8PN0waSNvroMtvAK6tY7g/wPdNWGEP+JTNIBr6A==", + "requires": { + "config-chain": "^1.1.12", + "editorconfig": "^0.15.3", + "glob": "^7.1.3", + "mkdirp": "~1.0.3", + "nopt": "^4.0.3" + }, + "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", @@ -4675,7 +4721,6 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, "requires": { "brace-expansion": "^1.1.7" } @@ -4910,6 +4955,15 @@ "semver": "^5.3.0" } }, + "nopt": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/nopt/-/nopt-4.0.3.tgz", + "integrity": "sha512-CvaGwVMztSMJLOeXPrez7fyfObdZqNUK1cPAEzLHrTybIua9pMdmmPR5YwtfNftIOMv3DPUhFaxsZMNTQO20Kg==", + "requires": { + "abbrev": "1", + "osenv": "^0.1.4" + } + }, "normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", @@ -5026,7 +5080,6 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "dev": true, "requires": { "wrappy": "1" } @@ -5037,6 +5090,11 @@ "integrity": "sha1-hUNzx/XCMVkU/Jv8a9gjj92h7Cc=", "dev": true }, + "os-homedir": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz", + "integrity": "sha1-/7xJiDNuDoM94MFox+8VISGqf7M=" + }, "os-locale": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-3.1.0.tgz", @@ -5048,6 +5106,20 @@ "mem": "^4.0.0" } }, + "os-tmpdir": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", + "integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=" + }, + "osenv": { + "version": "0.1.5", + "resolved": "https://registry.npmjs.org/osenv/-/osenv-0.1.5.tgz", + "integrity": "sha512-0CWcCECdMVc2Rw3U5w9ZjqX6ga6ubk1xDVKxtBQPK7wis/0F2r9T6k4ydGYhecl7YUBxBVxhL5oisPsNxAPe2g==", + "requires": { + "os-homedir": "^1.0.0", + "os-tmpdir": "^1.0.0" + } + }, "p-cancelable": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-1.1.0.tgz", @@ -5190,8 +5262,7 @@ "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", - "dev": true + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" }, "path-key": { "version": "2.0.1", @@ -5373,6 +5444,11 @@ "integrity": "sha1-mEcocL8igTL8vdhoEputEsPAKeM=", "dev": true }, + "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", @@ -5392,8 +5468,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=" }, "public-encrypt": { "version": "4.0.3", @@ -5789,8 +5864,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", @@ -5914,6 +5988,11 @@ "nanoid": "^2.1.0" } }, + "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", @@ -7067,8 +7146,7 @@ "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "dev": true + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" }, "xss": { "version": "1.0.6", diff --git a/package.json b/package.json @@ -1,7 +1,7 @@ { "name": "@striven-erp/striven-editor", "private": false, - "version": "3.4.1", + "version": "3.4.96", "description": "The lightweight zero-dependency Striven WYSIWYG editor.", "main": "./dist", "scripts": { @@ -11,7 +11,7 @@ "dev": "npx webpack -w", "meta-server": "node metaserver", "gh-pages": "cd demo && npm run build && cd .. && node gh-pages", - "new-publish": "npm publish --access=public && echo DON'T FORGET TO UPDATE GITHUB RELEASE", + "new-publish": "npm publish --access=public", "doc": "node doc" }, "repository": { @@ -57,6 +57,7 @@ }, "dependencies": { "@simonwep/pickr": "^1.5.0", + "js-beautify": "^1.11.0", "linkifyjs": "^2.1.9" } } diff --git a/src/blowupelement.js b/src/blowupelement.js @@ -0,0 +1,49 @@ +export default function blowUpElement(el, bg = '#fff', onExpand) { + function reset() { + el.dataset.expanded = 'false'; + el.style.height = null; + el.style.overflow = null; + el.style.position = null; + el.style.top = null; + el.style.bottom = null; + el.style.left = null; + el.style.right = null; + el.style.backgroundColor = null; + el.style.zIndex = null; + el.oncollapse && el.oncollapse(); + } + + el.dataset.expanded = 'true'; + el.style.height = 'auto'; + el.style.overflow = 'auto'; + el.style.position = 'fixed'; + el.style.top = '0'; + el.style.bottom = '0'; + el.style.left = '0'; + el.style.right = '0'; + el.style.backgroundColor = bg; + el.style.zIndex = '1050'; + + onExpand && onExpand(el); + el.collapse = () => reset(); + + window.addEventListener('keydown', (e) => { + if (e.key) { + switch (e.key) { + case 'Escape': + el.dataset.expanded === 'true' && reset(); + break; + default: + break; + } + } else { + switch (e.keyCode) { + case 13: + el.dataset.expanded === 'true' && reset(); + break; + default: + break; + } + } + }) +} diff --git a/src/createSVG.js b/src/createSVG.js @@ -0,0 +1,20 @@ +/* + * Constructs an SVG from a viewBox and d property + * @param {Object} Object with viewBox and d property + * @param {String} Fill color for the svg + * @param {Number} Height of the SVG + * @param {Width} Width of the SVG + * @returns {HTMLElement} Returns SVG Element + */ +export default function(svgData, fillColor = '#000', height = 16, width = 16) { + const {viewBox, d} = svgData; + const xmlns = 'http://www.w3.org/2000/svg'; + + const icon = document.createElement('div'); + const svg = `<svg width="${width}" height="${height}" viewBox="${viewBox}" xmlns="${xmlns}">`; + const path = `<path fill="${fillColor}" d="${d}"/>`; + + icon.innerHTML = `${svg}${path}</svg>`; + + return icon.querySelector('svg'); +} diff --git a/src/defaults.js b/src/defaults.js @@ -84,7 +84,6 @@ const OPTIONGROUPS = { 'M1760 896q14 0 23 9t9 23v64q0 14-9 23t-23 9h-1728q-14 0-23-9t-9-23v-64q0-14 9-23t23-9h1728zm-1277-64q-28-35-51-80-48-98-48-188 0-181 134-309 133-127 393-127 50 0 167 19 66 12 177 48 10 38 21 118 14 123 14 183 0 18-5 45l-12 3-84-6-14-2q-50-149-103-205-88-91-210-91-114 0-182 59-67 58-67 146 0 73 66 140t279 129q69 20 173 66 58 28 95 52h-743zm507 256h411q7 39 7 92 0 111-41 212-23 56-71 104-37 35-109 81-80 48-153 66-80 21-203 21-114 0-195-23l-140-40q-57-16-72-28-8-8-8-22v-13q0-108-2-156-1-30 0-68l2-37v-44l102-2q15 34 30 71t22.5 56 12.5 27q35 57 80 94 43 36 105 57 59 22 132 22 64 0 139-27 77-26 122-86 47-61 47-129 0-84-81-157-34-29-137-71z', }, }, - ], }, listOptions: { @@ -186,6 +185,15 @@ const OPTIONGROUPS = { d: 'M576 1376v-192q0-14-9-23t-23-9h-320q-14 0-23 9t-9 23v192q0 14 9 23t23 9h320q14 0 23-9t9-23zm0-384v-192q0-14-9-23t-23-9h-320q-14 0-23 9t-9 23v192q0 14 9 23t23 9h320q14 0 23-9t9-23zm512 384v-192q0-14-9-23t-23-9h-320q-14 0-23 9t-9 23v192q0 14 9 23t23 9h320q14 0 23-9t9-23zm-512-768v-192q0-14-9-23t-23-9h-320q-14 0-23 9t-9 23v192q0 14 9 23t23 9h320q14 0 23-9t9-23zm512 384v-192q0-14-9-23t-23-9h-320q-14 0-23 9t-9 23v192q0 14 9 23t23 9h320q14 0 23-9t9-23zm512 384v-192q0-14-9-23t-23-9h-320q-14 0-23 9t-9 23v192q0 14 9 23t23 9h320q14 0 23-9t9-23zm-512-768v-192q0-14-9-23t-23-9h-320q-14 0-23 9t-9 23v192q0 14 9 23t23 9h320q14 0 23-9t9-23zm512 384v-192q0-14-9-23t-23-9h-320q-14 0-23 9t-9 23v192q0 14 9 23t23 9h320q14 0 23-9t9-23zm0-384v-192q0-14-9-23t-23-9h-320q-14 0-23 9t-9 23v192q0 14 9 23t23 9h320q14 0 23-9t9-23zm128-320v1088q0 66-47 113t-113 47h-1344q-66 0-113-47t-47-113v-1088q0-66 47-113t113-47h1344q66 0 113 47t47 113z', }, + fullscreen: { + viewBox: '0 0 1792 1792', + d: + 'M883 1056q0 13-10 23l-332 332 144 144q19 19 19 45t-19 45-45 19h-448q-26 0-45-19t-19-45v-448q0-26 19-45t45-19 45 19l144 144 332-332q10-10 23-10t23 10l114 114q10 10 10 23zm781-864v448q0 26-19 45t-45 19-45-19l-144-144-332 332q-10 10-23 10t-23-10l-114-114q-10-10-10-23t10-23l332-332-144-144q-19-19-19-45t19-45 45-19h448q26 0 45 19t19 45z', + }, + html: { + viewBox: '0 0 2048 1792', + d: 'M681 1399l-50 50q-10 10-23 10t-23-10l-466-466q-10-10-10-23t10-23l466-466q10-10 23-10t23 10l50 50q10 10 10 23t-10 23l-393 393 393 393q10 10 10 23t-10 23zm591-1067l-373 1291q-4 13-15.5 19.5t-23.5 2.5l-62-17q-13-4-19.5-15.5t-2.5-24.5l373-1291q4-13 15.5-19.5t23.5-2.5l62 17q13 4 19.5 15.5t2.5 24.5zm657 651l-466 466q-10 10-23 10t-23-10l-50-50q-10-10-10-23t10-23l393-393-393-393q-10-10-10-23t10-23l50-50q10-10 23-10t23 10l466 466q10 10 10 23t-10 23z' + } }, { removeFormat: { @@ -219,6 +227,8 @@ const DEFAULTOPTIONS = [ 'link', 'image', 'table', + 'html', + 'fullscreen' ]; const ACTIVEOPTIONCOLOR = '#777777'; @@ -239,7 +249,24 @@ const FONTSIZES = { 4: '14pt', 5: '18pt', 6: '24pt', - 7: '36pt' + 7: '36pt', +}; + +const COLLAPSEICON = { + viewBox: '0 0 1792 1792', + d: + 'M896 960v448q0 26-19 45t-45 19-45-19l-144-144-332 332q-10 10-23 10t-23-10l-114-114q-10-10-10-23t10-23l332-332-144-144q-19-19-19-45t19-45 45-19h448q26 0 45 19t19 45zm755-672q0 13-10 23l-332 332 144 144q19 19 19 45t-19 45-45 19h-448q-26 0-45-19t-19-45v-448q0-26 19-45t45-19 45 19l144 144 332-332q10-10 23-10t23 10l114 114q10 10 10 23z', +}; + +const EXPANDICON = { + viewBox: '0 0 1792 1792', + d: + 'M883 1056q0 13-10 23l-332 332 144 144q19 19 19 45t-19 45-45 19h-448q-26 0-45-19t-19-45v-448q0-26 19-45t45-19 45 19l144 144 332-332q10-10 23-10t23 10l114 114q10 10 10 23zm781-864v448q0 26-19 45t-45 19-45-19l-144-144-332 332q-10 10-23 10t-23-10l-114-114q-10-10-10-23t10-23l332-332-144-144q-19-19-19-45t19-45 45-19h448q26 0 45 19t19 45z', +}; + +const DESIGNICON = { + viewBox: '0 0 1792 1792', + d: 'M491 1536l91-91-235-235-91 91v107h128v128h107zm523-928q0-22-22-22-10 0-17 7l-542 542q-7 7-7 17 0 22 22 22 10 0 17-7l542-542q7-7 7-17zm-54-192l416 416-832 832h-416v-416zm683 96q0 53-37 90l-166 166-416-416 166-165q36-38 90-38 53 0 91 38l235 234q37 39 37 91z' } export { @@ -252,4 +279,7 @@ export { HILITECOLORICON, FONTNAMES, FONTSIZES, + COLLAPSEICON, + EXPANDICON, + DESIGNICON }; diff --git a/src/striveneditor.css b/src/striveneditor.css @@ -64,24 +64,64 @@ display: flex; justify-content: space-between; flex-wrap: wrap; - height: 40px; + height: 40px; transition: height 200ms; } -.striven-editor .se-toolbar.se-toolbar-top { - align-items: flex-start; +.striven-editor .se-toolbar .se-divider-section { + height: 100%; + width: 5px; + display: flex; + justify-content: center; + align-items: center; } -.striven-editor .se-toolbar.se-toolbar-bottom { - align-items: flex-end; +.striven-editor .se-toolbar .se-divider-section .se-divider-bar { + height: 50%; + width: 1px; + background-color: #ddd; +} + +.striven-editor .se-html .se-toolbar-group { + display: none !important; +} + +.striven-editor .se-html .se-toolbar-selection { + display: none !important; +} + +.striven-editor .se-html .se-divider-section { + display: none !important; +} + +.striven-editor .se-html .se-toolbar-menu { + display: none !important; +} + +.striven-editor .se-html { + display: flex !important; + justify-content: flex-start !important; + align-items: center !important; +} + +.striven-editor .se-html #toolbar-fullscreen{ + display: block!important; } .striven-editor .se-toolbar-close { height: 0px; } +.striven-editor .se-toolbar.se-toolbar-top { + align-items: flex-start; +} + +.striven-editor .se-toolbar.se-toolbar-bottom { + align-items: flex-end; +} + .striven-editor .se-toolbar-options { - display: flex; + display: flex; margin-right: 10px; } @@ -131,6 +171,15 @@ z-index: 1000; } +/* +.striven-editor .se-toolbar-group:after { + content: ''; + margin: 0 auto; + padding-bottom: 5px; + border-right: 1px solid #ddd; +} +*/ + /* .striven-editor .se-toolbar-group[data-open='true'] .se-toolbar-option { background-color: #fff; @@ -164,7 +213,7 @@ display: flex; justify-content: center; align-items: center; - background-color: transparent; + background-color: transparent; cursor: pointer; padding: 0 5px; transition: background-color 100ms; @@ -179,12 +228,6 @@ background-color: transparent; } -@media (max-width: 500px){ - .striven-editor .se-toolbar .se-toolbar-selection { - display: none; - } -} - /* .striven-editor .se-toolbar .se-toolbar-selection .se-toolbar-option { border: 1px solid #ddd; @@ -216,15 +259,15 @@ } .striven-editor .se-toolbar .se-toolbar-colorholder:hover { - background-color: #ddd; + background-color: #ddd; } .striven-editor .se-toolbar-popup-option { - padding: 10px 15px; - background-color: transparent; - transition: background-color 200ms; - font-size: 16px; - cursor: pointer; + padding: 10px 15px; + background-color: transparent; + transition: background-color 200ms; + font-size: 16px; + cursor: pointer; } .striven-editor .se-toolbar-popup-option:hover { @@ -343,6 +386,14 @@ opacity: 1; } +.striven-editor .se-popup-header { + color: #888; + font-size: 12px; + margin: 0; + border-bottom: 1px solid #ddd; + padding-bottom: 3px; +} + .striven-editor .se-popup > .se-popup-form { display: flex; justify-content: space-between; @@ -351,8 +402,7 @@ .striven-editor .se-popup > .se-popup-form > .se-form-label { margin: 8px 10px 8px 0; - font-size: 14px; - width: 100%; + font-size: 12px; text-align: right; margin-right: 10px; } @@ -362,6 +412,12 @@ padding: 5px; } +@media (min-width: 900px) { + .striven-editor .se-popup > .se-popup-form > .se-form-input { + width: 250px; + } +} + .striven-editor .se-popup-button-container { float: right; } @@ -377,8 +433,8 @@ } .striven-editor .se-popup-button.se-button-primary { - border-color: #4cae4c; - background-color: #5cb85c; + border-color: #357ebd; + background-color: #428bca; color: #fff; } .striven-editor .se-popup-button.se-button-secondary { @@ -389,8 +445,8 @@ .striven-editor .se-popup-button.se-button-primary:hover, .striven-editor .se-popup-button.se-button-primary:active, .striven-editor .se-popup-button.se-button-primary:focus { - background-color: #4cae4c; - border-color: #398439; + background-color: #285e8e; + border-color: #285e8e; } .striven-editor .se-popup-button.se-button-secondary:hover, .striven-editor .se-popup-button.se-button-secondary:active, @@ -398,4 +454,3 @@ background-color: #d4d4d4; border-color: #8c8c8c; } - diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -9,10 +9,15 @@ import { HILITECOLORICON, FONTNAMES, FONTSIZES, + COLLAPSEICON, + EXPANDICON, + DESIGNICON, } from './defaults.js'; // Helpers +import createSVG from './createSVG'; import denormalizeCamel from './denormalizeCamel'; +import blowUpElement from './blowupelement'; // Polyfills import ResizeObserver from 'resize-observer-polyfill'; @@ -21,6 +26,7 @@ import ResizeObserver from 'resize-observer-polyfill'; import linkify from 'linkifyjs/element'; import '@simonwep/pickr/dist/themes/classic.min.css'; import Pickr from '@simonwep/pickr'; +import sourceFormatter from 'js-beautify'; /* Represents an instance of the Striven Editor */ export default class StrivenEditor { @@ -31,27 +37,31 @@ export default class StrivenEditor { _bindContenteditableOnChange(el) { const se = this; - el.addEventListener('focus', function () { + el.addEventListener('focus', function() { if (el.data_orig === undefined) { el.data_orig = el.innerHTML; } }); - el.addEventListener('blur', function () { - + el.addEventListener('blur', function() { setTimeout(() => { - const menus = se.editor.getElementsByClassName('se-popup-open'); - const inputs = se.editor.getElementsByTagName('input'); + if (!se.toolbar.classList.contains('se-html')) { + const menus = se.editor.getElementsByClassName('se-popup-open'); + const inputs = se.editor.getElementsByTagName('input'); - const actives = [...menus, ...inputs, se.body, se.toolbar, se.editor]; + const actives = [...menus, ...inputs, se.body, se.toolbar, se.editor]; - if (el.innerHTML != el.data_orig && !se.toolbarClick && !actives.includes(document.activeElement) && !menus.length) { - se.options.change(se.getContent()); - delete el.data_orig; + if ( + el.innerHTML != el.data_orig && + !se.toolbarClick && + !actives.includes(document.activeElement) && + !menus.length + ) { + se.options.change(se.getContent()); + delete el.data_orig; + } } - }, 500) - - + }, 500); }); se.toolbarClick = false; @@ -136,6 +146,7 @@ export default class StrivenEditor { activeOptionColor: ACTIVEOPTIONCOLOR, fontNames: FONTNAMES, fileUpload: true, + canTab: true, }; } @@ -179,6 +190,12 @@ export default class StrivenEditor { // Initialze with the value property in the options this.setContent(this.options.value || ''); + window.addEventListener('click', () => { + if (!this.editor.contains(document.activeElement)) { + this.closeAllMenus(); + } + }); + // Toolbar Hide if (this.options.toolbarHide) { // Hide the toolbar template if there is one @@ -217,8 +234,12 @@ export default class StrivenEditor { } }, 200); }; + } else { + this.toolbar.style.boxShadow = '#ddd -1px 2px 3px 0px'; + this.toolbar.style.height = 'fit-content'; } + const se = this; // Toolbar Options this.toolbarOptions.forEach(optionEl => { // Execute Toolbar Commands @@ -226,6 +247,10 @@ export default class StrivenEditor { // Bind the toolbar commands click hander optionEl.onclick = e => { + if (!this.browser.isSafari()) { + this.range = this.getRange(); + } + // Get the document execute command const command = optionEl.id.split('-').pop(); @@ -267,14 +292,9 @@ export default class StrivenEditor { this.toolbarOptions.forEach(o => o.classList.remove('se-toolbar-option-active'), ); - - // Reset font options - this.fontName && - (this.fontName.textContent = this.options.fontNames[0]); - this.fontSize && (this.fontSize.textContent = FONTSIZES[3]); - this.foreColor && this.foreColor.setAttribute('fill', '#000'); - this.hiliteColor && this.hiliteColor.setAttribute('fill', '#fff'); break; + case 'indent': + setTimeout(() => this.setRange(this.range), 0); default: this.body.focus(); this.executeCommand(command); @@ -285,6 +305,28 @@ export default class StrivenEditor { }; }); + // Add dividers + function constructDivider() { + const divider = document.createElement('div'); + divider.classList.add('se-divider-section'); + + const bar = document.createElement('div'); + bar.classList.add('se-divider-bar'); + + divider.append(bar); + return divider; + } + + const areas = [ + ...se.toolbarGroups, + ...se.toolbar.querySelectorAll('.se-toolbar-selection'), + ]; + areas.forEach(a => { + if (a.querySelector('.se-toolbar-option')) { + a.after(constructDivider()); + } + }); + // Construct editor elements this.toolbar && this.editor.appendChild(this.toolbar); this.body && this.editor.appendChild(this.body); @@ -410,7 +452,7 @@ export default class StrivenEditor { optionSpan.setAttribute('title', 'Background Color'); break; default: - optionSpan.setAttribute('title', denormalizeCamel(option)) + optionSpan.setAttribute('title', denormalizeCamel(option)); break; } toolbarGroup.appendChild(optionSpan); @@ -451,17 +493,17 @@ export default class StrivenEditor { } }); - const miscOptions = toolbar.querySelector('#group-options') + const miscOptions = toolbar.querySelector('#group-options'); // toolbar group for custom options const customOptions = this.options.toolbarOptions.filter( option => typeof option === 'object', ); if (customOptions.length > 0) { customOptions.forEach(opt => { - const { icon, handler, title } = opt; + const {icon, handler, title} = opt; if (typeof icon === 'object') { - const option = this.constructSVG({ viewBox: icon.viewBox, d: icon.d }); + const option = this.constructSVG({viewBox: icon.viewBox, d: icon.d}); option.classList.add('se-toolbar-option'); option.onclick = () => handler(option); @@ -539,7 +581,7 @@ export default class StrivenEditor { const menu = initMenu('fontName'); menu.dataset.init = 'true'; - se.options.fontNames.forEach(f => { + ['(inherited font)', ...se.options.fontNames].forEach(f => { const fontOption = document.createElement('div'); fontOption.classList.add('se-toolbar-popup-option'); @@ -547,14 +589,39 @@ export default class StrivenEditor { fontOption.style.fontFamily = f; fontOption.onclick = e => { - const fontselect = e.target.textContent; + let fontselect = e.target.textContent; select.textContent = fontselect; menu.close(); const refocus = se.body.onfocus; se.body.onfocus = () => { - se.body.textContent && se.setRange(se.range); - se.execFontStates(); + 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); + } + } + + se.setRange(se.range); + + if (!se.getContent()) { + const enabler = () => { + setTimeout(() => { + execute(); + se.body.removeEventListener('keydown', enabler); + }); + }; + + se.body.addEventListener('keydown', enabler); + } + + setTimeout(() => execute(), 0); + if (se.scrollPosition && !se.browser.isEdge()) { se.body.scrollTo(se.scrollPosition.x, se.scrollPosition.y); } @@ -575,8 +642,8 @@ export default class StrivenEditor { const sizes = FONTSIZES; - Object.keys(sizes).forEach(size => { - const s = sizes[size]; + ['(inherited size)', ...Object.keys(sizes)].forEach(size => { + let s = sizes[size] || '(inherited size)'; const fontOption = document.createElement('div'); @@ -584,6 +651,20 @@ export default class StrivenEditor { fontOption.textContent = s; fontOption.onclick = e => { + function execute() { + let execSize = size; + + if (size === '(inherited size)') { + execSize = 3; + } + + if (se.browser.isEdge() || se.browser.isFirefox()) { + document.execCommand('fontSize', false, execSize); + } else { + document.execCommand('fontSize', true, execSize); + } + } + const fontsize = e.target.textContent; select.textContent = fontsize; @@ -593,8 +674,21 @@ export default class StrivenEditor { const refocus = se.body.focus; se.body.onfocus = () => { - se.body.textContent && se.setRange(se.range); - se.execFontStates(); + se.setRange(se.range); + + if (!se.getContent()) { + const enabler = () => { + setTimeout(() => { + execute(); + se.body.removeEventListener('keydown', enabler); + }, 0); + }; + + se.body.addEventListener('keydown', enabler); + } + + setTimeout(() => execute(), 0); + if (se.scrollPosition && !se.browser.isEdge()) { se.body.scrollTo(se.scrollPosition.x, se.scrollPosition.y); } @@ -653,8 +747,8 @@ export default class StrivenEditor { fontOption.onclick = e => { menu.close(); se.body.focus(); - se.setRange(); + if (se.browser.isFirefox() || se.browser.isEdge()) { document.execCommand('removeFormat', false); } else { @@ -662,7 +756,6 @@ export default class StrivenEditor { } se.executeCommand('formatBlock', s.command); - se.execFontStates(); }; menu.append(fontOption); @@ -685,7 +778,10 @@ export default class StrivenEditor { // Toggle the fontselect popup on the select click fontSelect.onclick = () => { - se.range = se.getRange(); + if (!se.browser.isSafari()) { + se.range = se.getRange(); + } + if (menu.dataset.active === 'true') { menu.close(); } else { @@ -694,7 +790,7 @@ export default class StrivenEditor { }; // Set the select to initialize on the first font name - selectedFont.textContent = this.options.fontNames[0]; + selectedFont.textContent = '(inherited font)'; fontSelect.setAttribute('id', 'toolbar-fontName'); @@ -713,7 +809,10 @@ export default class StrivenEditor { const menu = initFontSizeMenu(selectedFontSize); fontSizeSelect.onclick = () => { - se.range = se.getRange(); + if (!se.browser.isSafari()) { + se.range = se.getRange(); + } + if (menu.dataset.active === 'true') { menu.close(); } else { @@ -722,7 +821,7 @@ export default class StrivenEditor { }; fontSizeSelect.setAttribute('id', 'toolbar-fontSize'); - selectedFontSize.textContent = FONTSIZES[3]; + selectedFontSize.textContent = '(inherited size)'; fontSizeSelect.classList.add('se-toolbar-selection'); selectedFontSize.classList.add('se-toolbar-option'); @@ -738,7 +837,10 @@ export default class StrivenEditor { const menu = initFontFormatMenu(); fontFormatSelect.onclick = () => { - se.range = se.getRange(); + if (!se.browser.isSafari()) { + se.range = se.getRange(); + } + if (menu.dataset.active === 'true') { menu.close(); } else { @@ -782,6 +884,14 @@ export default class StrivenEditor { this._bindContenteditableOnChange(body); } + // Execute this function on mouseup and keyup + const execRange = () => { + const current = this.getRange(); + if (current) { + this.range = current; + } + }; + // Paste Handler body.onpaste = e => { // Convert envoding to file @@ -795,7 +905,7 @@ export default class StrivenEditor { u8arr[n] = bstr.charCodeAt(n); } - const file = new File([u8arr], filename, { type: mime }); + const file = new File([u8arr], filename, {type: mime}); return new File([u8arr], `${file.name}.${file.type.split('/').pop()}`, { type: file.type, }); @@ -891,7 +1001,7 @@ export default class StrivenEditor { // get meta data if (this.options.metaUrl) { this.getMeta(string).then(res => { - const { url, title, image, description } = res; + const {url, title, image, description} = res; url && title && image && @@ -915,8 +1025,14 @@ export default class StrivenEditor { body.onkeydown = e => { switch (e.key) { - case 'Tab': - break; + // case 'Tab': + // if (this.options.canTab) { + // e.shiftKey + // ? se.executeCommand('outdent') + // : se.executeCommand('indent'); + // e.preventDefault(); + // } + // break; case 'Shift': break; case 'Backspace': @@ -950,33 +1066,39 @@ export default class StrivenEditor { const bodyKeyup = body.onkeyup; body.onkeyup = e => { bodyKeyup && bodyKeyup(); - se.range = se.getRange(); + execRange(); - switch (e.key) { - case 'Enter': - se.options.onEnter && se.options.onEnter(e); - default: - break; + if (e.key) { + switch (e.key) { + case 'Enter': + se.options.onEnter && se.options.onEnter(e); + break; + default: + break; + } } this.setFontStates(); this.toolbarState(); }; - const bodyMouseUp = body.onmouseup; - body.onmouseup = e => { - bodyMouseUp && bodyMouseUp(); - this.range = this.getRange(); - }; - const bodyFocus = body.onfocus; body.onfocus = e => { !this.browser.isEdge() && this.setRange(); - if (!this.getContent()) { - this.setContent('&nbsp;'); + window.addEventListener('mouseup', execRange); + + this.editor.classList.add('se-focus'); + + if (this.scrollPosition && !this.browser.isEdge()) { + body.scrollTo(this.scrollPosition); } + bodyFocus && bodyFocus(); + }; + + // Bind state management event + body.addEventListener('focus', () => { // disables all states this.options.toolbarOptions.forEach(opt => { if (typeof opt === 'string') { @@ -1001,24 +1123,13 @@ export default class StrivenEditor { this.getActiveOptions().forEach(opt => { !document.queryCommandState(opt) && this.executeCommand(opt); }); - - this.execFontStates(); - this.editor.classList.add('se-focus'); - - if (this.scrollPosition && !this.browser.isEdge()) { - body.scrollTo(this.scrollPosition); - } - - bodyFocus && bodyFocus(); - }; + }); const bodyBlur = body.onblur; body.onblur = e => { this.editor.classList.remove('se-focus'); - if (this.getContent() === '&nbsp;') { - this.setContent(''); - } + window.removeEventListener('mouseup', execRange); this.scrollPosition = { y: body.scrollTop, @@ -1033,6 +1144,7 @@ export default class StrivenEditor { const bodyClick = body.onclick; body.onclick = () => { + this.closeAllMenus(); this.setFontStates(); body.textContent && this.toolbarState(); bodyClick && bodyClick; @@ -1051,13 +1163,17 @@ export default class StrivenEditor { * @returns {HTMLElement} The StrivenEditor link menu */ initLinkMenu() { + const se = this; + const linkMenu = document.createElement('div'); + const linkMenuHeader = document.createElement('p'); const linkMenuForm = document.createElement('div'); const linkMenuButtons = document.createElement('div'); const linkMenuButton = document.createElement('button'); const linkMenuCloseButton = document.createElement('button'); const linkMenuFormLabel = document.createElement('p'); const linkMenuFormInput = document.createElement('input'); + const linkMenuCheck = document.createElement('input'); function resetInput() { linkMenuFormInput.value = 'http://'; @@ -1070,11 +1186,13 @@ export default class StrivenEditor { linkMenuForm.classList.add('se-popup-form'); linkMenuFormLabel.classList.add('se-form-label'); - linkMenuFormLabel.textContent = 'Web Address'; + linkMenuFormLabel.textContent = 'URL'; linkMenuFormInput.classList.add('se-form-input'); + se.options.useBootstrap && linkMenuFormInput.classList.add('form-control'); linkMenuFormInput.type = 'text'; linkMenuFormInput.placeholder = 'Insert a Link'; + resetInput(); linkMenuButton.type = 'button'; @@ -1090,6 +1208,7 @@ export default class StrivenEditor { linkMenuButton.onclick = e => { const linkValue = linkMenuFormInput.value; + this.body.focus(); this.setRange(); if (linkValue) { @@ -1099,12 +1218,47 @@ export default class StrivenEditor { document.execCommand('createLink', true, linkValue); } - const links = [...this.body.querySelectorAll(`a[href="${linkValue}"]`)]; - links.forEach(link => link.setAttribute('target', '_blank')); + const insertedLink = this.getRange().commonAncestorContainer + .parentElement; + if (insertedLink) { + windowRowInput.checked && + insertedLink.setAttribute('target', '_blank'); + windowRowInput.checked = true; + + if (textRowInput.value) { + insertedLink.textContent = textRowInput.value; + textRowInput.value = ''; + } + + insertedLink.onmouseover = ({ctrlKey}) => { + const redirectClick = (e) => { + e.preventDefault(); + + const redirectLink = document.createElement('a'); + redirectLink.setAttribute('target', '_blank'); + redirectLink.setAttribute('href', insertedLink.getAttribute('href')); + redirectLink.setAttribute('style', 'display: none;'); + document.body.append(redirectLink); + redirectLink.click(); + redirectLink.remove(); + } + + if(ctrlKey){ + insertedLink.style.cursor = 'pointer'; + + insertedLink.addEventListener('click', redirectClick); + } else { + insertedLink.style.cursor = null; + insertedLink.removeEventListener('click', redirectClick); + } + }; + + insertedLink.parsed = true; + } if (this.options.metaUrl && this.validURL(linkValue)) { this.getMeta(linkValue).then(res => { - const { url, image, title, description } = res; + const {url, image, title, description} = res; url && image && title && @@ -1126,10 +1280,10 @@ export default class StrivenEditor { } // Remove contenteditable for firefox - if (!this.browser.isFirefox()) { - const bodyLinks = this.body.querySelectorAll('a'); - [...bodyLinks].forEach(link => (link.contentEditable = 'false')); - } + // if (!this.browser.isFirefox()) { + // const bodyLinks = this.body.querySelectorAll('a'); + // [...bodyLinks].forEach(link => (link.contentEditable = 'false')); + // } // trigger input event if (this.body.oninput) { @@ -1151,15 +1305,66 @@ export default class StrivenEditor { resetInput(); }; - // linkMenuForm.appendChild(linkMenuFormLabel); + linkMenuHeader.classList.add('se-popup-header'); + linkMenuHeader.innerText = 'Insert Link'; + + linkMenu.appendChild(linkMenuHeader); + + linkMenuForm.appendChild(linkMenuFormLabel); linkMenuForm.appendChild(linkMenuFormInput); + const textRow = linkMenuForm.cloneNode(true); + const textRowLabel = textRow.querySelector('.se-form-label'); + const textRowInput = textRow.querySelector('.se-form-input'); + + if (textRowLabel) { + textRowLabel.innerText = 'Text'; + } + + if (textRowInput) { + textRowInput.value = ''; + textRowInput.placeholder = 'Text content'; + } + + const windowRow = linkMenuForm.cloneNode(true); + const windowRowLabel = windowRow.querySelector('.se-form-label'); + const windowRowInput = windowRow.querySelector('.se-form-input'); + + if (windowRow) { + windowRow.setAttribute( + 'style', + 'justify-content: flex-end; align-items: center; flex-direction: row-reverse;', + ); + } + + if (windowRowLabel) { + windowRowLabel.innerText = 'Open in new window?'; + + windowRowLabel.setAttribute('style', 'margin: 3px 5px 0px 5px;'); + } + + if (windowRowInput) { + windowRowInput.checked = true; + windowRowInput.setAttribute('type', 'checkbox'); + windowRowInput.setAttribute('style', 'width: auto'); + } + linkMenu.appendChild(linkMenuForm); + linkMenu.appendChild(textRow); + linkMenu.appendChild(windowRow); linkMenuButtons.appendChild(linkMenuButton); linkMenuButtons.appendChild(linkMenuCloseButton); linkMenu.appendChild(linkMenuButtons); + + [...linkMenu.getElementsByTagName('input')].forEach(inp => { + inp.onkeydown = e => { + if (e.key === 'Enter') { + e.preventDefault(); + } + }; + }); return linkMenu; } @@ -1168,7 +1373,10 @@ export default class StrivenEditor { * @returns {HTMLElement} The StrivenEditor image menu */ initImageMenu() { + const se = this; + const imageMenu = document.createElement('div'); + const imageMenuHeader = document.createElement('p'); const imageMenuForm = document.createElement('div'); const imageMenuButtons = document.createElement('div'); const imageMenuButton = document.createElement('button'); @@ -1186,6 +1394,9 @@ export default class StrivenEditor { imageMenuFormLabel.textContent = 'Image URL'; imageMenuFormSourceInput.classList.add('se-form-input'); + se.options.useBootstrap && + imageMenuFormSourceInput.classList.add('form-control'); + imageMenuFormSourceInput.type = 'text'; imageMenuFormSourceInput.placeholder = 'Insert a Link'; @@ -1223,17 +1434,14 @@ export default class StrivenEditor { imageMenuWidthFormLabel.textContent = 'Width'; imageMenuButton.onclick = e => { + this.body.focus(); + this.setRange(); + const linkValue = imageMenuFormSourceInput.value; const heightValue = imageMenuHeightFormInput.value; const widthValue = imageMenuWidthFormInput.value; if (linkValue) { - if (!this.body.textContent) { - this.body.focus(); - } - - this.setRange(); - if (this.browser.isFirefox() || this.browser.isEdge()) { document.execCommand('insertImage', false, linkValue); } else { @@ -1261,6 +1469,12 @@ export default class StrivenEditor { this.body.focus(); this.closeImageMenu(); }; + + imageMenuHeader.classList.add('se-popup-header'); + imageMenuHeader.innerText = 'Insert Image'; + + imageMenu.appendChild(imageMenuHeader); + imageMenu.appendChild(imageMenuHeightForm); imageMenu.appendChild(imageMenuWidthForm); imageMenu.appendChild(imageMenuForm); @@ -1270,11 +1484,21 @@ export default class StrivenEditor { imageMenu.appendChild(imageMenuButtons); + [...imageMenu.getElementsByTagName('input')].forEach(inp => { + inp.onkeydown = e => { + if (e.key === 'Enter') { + e.preventDefault(); + } + }; + }); return imageMenu; } initTableMenu() { + const se = this; + const tableMenu = document.createElement('div'); + const tableMenuHeader = document.createElement('p'); const tableMenuForm = document.createElement('div'); const tableMenuButtons = document.createElement('div'); const tableMenuButton = document.createElement('button'); @@ -1292,6 +1516,8 @@ export default class StrivenEditor { tableMenuFormLabel.textContent = 'Columns'; tableMenuFormColsInput.classList.add('se-form-input'); + se.options.useBootstrap && + tableMenuFormColsInput.classList.add('form-control'); tableMenuFormColsInput.type = 'text'; tableMenuFormColsInput.placeholder = 'Table Columns'; @@ -1307,7 +1533,7 @@ export default class StrivenEditor { 'se-button-secondary', ); - tableMenuButton.textContent = 'Create Table'; + tableMenuButton.textContent = 'Insert'; tableMenuCloseButton.textContent = 'Close'; tableMenuForm.appendChild(tableMenuFormLabel); @@ -1330,6 +1556,7 @@ export default class StrivenEditor { } tableMenuButton.onclick = () => { + this.body.focus(); this.setRange(); if (tableMenuFormColsInput.value && tableMenuRowsInput.value) { @@ -1349,12 +1576,24 @@ export default class StrivenEditor { this.closeAllMenus(); }; + tableMenuHeader.classList.add('se-popup-header'); + tableMenuHeader.innerText = 'Insert Table'; + + tableMenu.appendChild(tableMenuHeader); + tableMenuButtons.appendChild(tableMenuButton); tableMenuButtons.appendChild(tableMenuCloseButton); tableMenu.appendChild(tableMenuRowsForm); tableMenu.appendChild(tableMenuForm); tableMenu.appendChild(tableMenuButtons); + [...tableMenu.getElementsByTagName('input')].forEach(inp => { + inp.onkeydown = e => { + if (e.key === 'Enter') { + e.preventDefault(); + } + }; + }); return tableMenu; } @@ -1379,7 +1618,7 @@ export default class StrivenEditor { window.addEventListener( 'dragover', - function (e) { + function(e) { e = e || event; e.preventDefault(); }, @@ -1388,7 +1627,7 @@ export default class StrivenEditor { window.addEventListener( 'drop', - function (e) { + function(e) { e = e || event; e.preventDefault(); }, @@ -1528,7 +1767,7 @@ export default class StrivenEditor { * @returns {HTMLElement} Returns the constructed svg */ constructSVG(svgData) { - const { viewBox, d } = svgData; + const {viewBox, d} = svgData; const fillColor = this.options.toolbarOptionFillColor ? this.options.toolbarOptionFillColor : '#333'; @@ -1553,6 +1792,38 @@ export default class StrivenEditor { if (!this.options.minimal) { function responsiveGroups(isResponsive) { + const isSmall = that.editor.offsetWidth < 600; + [...that.toolbar.querySelectorAll('.se-toolbar-selection')].forEach( + sel => (sel.style.display = isSmall ? 'none' : null), + ); + + [...that.toolbar.querySelectorAll('.se-divider-section')].forEach( + divider => (divider.style.display = isResponsive ? 'none' : null), + ); + + const fullscreenOption = that.toolbar.querySelector( + '#toolbar-fullscreen', + ); + if (fullscreenOption && !that.toolbar.classList.contains('se-html')) { + if (isResponsive) { + fullscreenOption.style.display = 'flex'; + fullscreenOption.style.alignItems = 'center'; + + const menuOptions = that.toolbar.querySelector('#menu-options'); + if (menuOptions) { + menuOptions.before(fullscreenOption); + } + } else { + fullscreenOption.style.display = null; + fullscreenOption.style.alignItems = null; + + const groupOptions = that.toolbar.querySelector('#group-options'); + if (groupOptions) { + groupOptions.append(fullscreenOption); + } + } + } + that.toolbarGroups.forEach(group => { if (group) { group.dataset.open = 'false'; @@ -1624,7 +1895,7 @@ export default class StrivenEditor { function setResponsive() { let responsive = window.matchMedia('(max-width: 700px)').matches; - responsiveGroups(responsive || that.editor.offsetWidth < 900); + responsiveGroups(responsive || that.editor.offsetWidth < 1000); } setResponsive(); @@ -1646,8 +1917,12 @@ export default class StrivenEditor { textDecorationMenu.onclick = () => { setTimeout(() => { - const textdecorationPopup = that.toolbar.querySelector('#group-textDecoration'); - let isOpen = textdecorationPopup.classList.contains('se-popup-open'); + const textdecorationPopup = that.toolbar.querySelector( + '#group-textDecoration', + ); + let isOpen = textdecorationPopup.classList.contains( + 'se-popup-open', + ); if (responsive) { if (isOpen) { @@ -1656,7 +1931,7 @@ export default class StrivenEditor { textDecorationGroup.classList.add('se-popup-open'); } } - }, 10) + }, 10); }; if (responsive) { @@ -1687,8 +1962,8 @@ export default class StrivenEditor { if ( this.toolbarTemplate ? this.toolbarTemplate.offsetWidth + - this.toolbarOptionsGroup.offsetWidth > - this.editor.offsetWidth + this.toolbarOptionsGroup.offsetWidth > + this.editor.offsetWidth : this.toolbarOptionsGroup.offsetWidth > this.editor.offsetWidth ) { responsiveMinimal(true); @@ -1761,10 +2036,12 @@ export default class StrivenEditor { * @returns {Range} Range of the window */ getRange() { - const selection = window.getSelection(); - if (selection) { - return window.getSelection().getRangeAt(0); - } + try { + const selection = window.getSelection(); + if (selection) { + return window.getSelection().getRangeAt(0); + } + } catch (e) {} } /** @@ -1775,8 +2052,8 @@ export default class StrivenEditor { getMeta(url) { return fetch(this.options.metaUrl, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ targetUrl: url }), + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify({targetUrl: url}), }).then(res => res.json()); } @@ -1788,8 +2065,8 @@ export default class StrivenEditor { getImage(imageEncoding) { return fetch(this.options.imageUrl, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ imageEncoding }), + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify({imageEncoding}), }).then(res => res.json()); } @@ -1962,6 +2239,18 @@ export default class StrivenEditor { this.linkMenu.dataset.active = 'true'; this.addPopupEscapeHandler(); + + const submitEvt = e => { + if (e.key === 'Enter') { + const submitEl = this.linkMenu.querySelector('button'); + submitEl.click(); + e.preventDefault(); + window.removeEventListener('keyup', this.linkMenu.submitEvt); + } + }; + + this.linkMenu.submitEvt = submitEvt; + window.addEventListener('keyup', this.linkMenu.submitEvt); } /** @@ -1978,6 +2267,18 @@ export default class StrivenEditor { this.imageMenu.dataset.active = 'true'; this.addPopupEscapeHandler(); + + const submitEvt = e => { + if (e.key === 'Enter') { + const submitEl = this.imageMenu.querySelector('button'); + submitEl.click(); + e.preventDefault(); + window.removeEventListener('keyup', this.imageMenu.submitEvt); + } + }; + + this.imageMenu.submitEvt = submitEvt; + window.addEventListener('keyup', this.imageMenu.submitEvt); } openTableMenu() { @@ -1991,12 +2292,25 @@ export default class StrivenEditor { this.tableMenu.dataset.active = 'true'; this.addPopupEscapeHandler(); + + const submitEvt = e => { + if (e.key === 'Enter') { + const submitEl = this.tableMenu.querySelector('button'); + submitEl.click(); + e.preventDefault(); + window.removeEventListener('keyup', this.tableMenu.submitEvt); + } + }; + + this.tableMenu.submitEvt = submitEvt; + window.addEventListener('keyup', this.tableMenu.submitEvt); } /** * Closes the editor's link menu popup */ closeLinkMenu() { + this.closeAllMenus(); this.linkMenu.classList.remove('se-popup-open'); this.linkMenu.dataset.active = 'false'; this.removePopupEscapeHandler(); @@ -2006,12 +2320,14 @@ export default class StrivenEditor { * Closes the editor's image menu popup */ closeImageMenu() { + this.closeAllMenus(); this.imageMenu.classList.remove('se-popup-open'); this.imageMenu.dataset.active = 'false'; this.removePopupEscapeHandler(); } closeTableMenu() { + this.closeAllMenus(); this.tableMenu.classList.remove('se-popup-open'); this.tableMenu.dataset.active = 'false'; this.removePopupEscapeHandler(); @@ -2022,7 +2338,13 @@ export default class StrivenEditor { */ closeAllMenus() { const popups = this.editor.getElementsByClassName('se-popup'); - [...popups].forEach(popup => popup.classList.remove('se-popup-open')); + [...popups].forEach(popup => { + popup.classList.remove('se-popup-open'); + + if (popup.submitEvt) { + window.removeEventListener('keyup', popup.submitEvt); + } + }); } /** @@ -2057,7 +2379,12 @@ export default class StrivenEditor { */ setContent(html) { this.clearContent(); - html && (this.body.innerHTML = html); + if (html) { + this.body.innerHTML = html; + [...this.body.querySelectorAll('a')].forEach( + link => (link.parsed = true), + ); + } } /** @@ -2065,13 +2392,15 @@ export default class StrivenEditor { * @param {Range} range Range to set the window with */ setRange(range) { - if (range) { - window.getSelection().removeAllRanges(); - window.getSelection().addRange(range); - } else { - window.getSelection().removeAllRanges(); - window.getSelection().addRange(this.range); - } + try { + if (range) { + window.getSelection().removeAllRanges(); + window.getSelection().addRange(range); + } else { + window.getSelection().removeAllRanges(); + window.getSelection().addRange(this.range); + } + } catch (e) {} } /** @@ -2124,10 +2453,12 @@ export default class StrivenEditor { !option.toLowerCase().includes('list') ) { if (document.queryCommandState(option)) { - toolbarOption.classList.add('se-toolbar-option-active'); + toolbarOption && + toolbarOption.classList.add('se-toolbar-option-active'); // toolbarOption.querySelector('path').setAttribute('fill', this.options.activeOptionColor); } else { - toolbarOption.classList.remove('se-toolbar-option-active'); + toolbarOption && + toolbarOption.classList.remove('se-toolbar-option-active'); // toolbarOption.querySelector('path').setAttribute('fill', '#333'); } } @@ -2139,14 +2470,20 @@ export default class StrivenEditor { * Update the font states on the toolbar */ setFontStates() { - this.fontSize && - !this.browser.isFirefox() && - (this.fontSize.textContent = FONTSIZES[document.queryCommandValue('fontSize')]); - this.fontName && - (this.fontName.textContent = document + if (this.fontSize && !this.browser.isFirefox()) { + this.fontSize.textContent = + FONTSIZES[document.queryCommandValue('fontSize')] || '(inherited size)'; + } + + if (this.fontName) { + const currentfont = document .queryCommandValue('fontName') .split(',')[0] - .replace(/\"/g, '')); + .replace(/\"/g, ''); + + this.fontName.textContent = currentfont || '(inherited font)'; + } + this.foreColor && this.foreColor.setAttribute( 'fill', @@ -2198,10 +2535,15 @@ export default class StrivenEditor { true, this.hiliteColor.getAttribute('fill'), ); + this.fontName && document.execCommand('fontName', true, this.fontName.textContent); this.fontSize && - document.execCommand('fontSize', true, this.fontSize.dataset.command); + document.execCommand( + 'fontSize', + true, + parseInt(this.fontSize.dataset.command), + ); } } @@ -2254,16 +2596,16 @@ export default class StrivenEditor { const vendor = ((navigator && navigator.vendor) || '').toLowerCase(); const comparator = { - '<': function (a, b) { + '<': function(a, b) { return a < b; }, - '<=': function (a, b) { + '<=': function(a, b) { return a <= b; }, - '>': function (a, b) { + '>': function(a, b) { return a > b; }, - '>=': function (a, b) { + '>=': function(a, b) { return a >= b; }, }; @@ -2373,6 +2715,10 @@ 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; const isLinkified = @@ -2453,8 +2799,8 @@ export default class StrivenEditor { pickrMenuButton.onclick = onclick; - pickrMenuActions.append(pickrMenuClose); pickrMenuActions.append(pickrMenuButton); + pickrMenuActions.append(pickrMenuClose); pickrMenu.append(pickrMenuActions); } @@ -2466,6 +2812,93 @@ export default class StrivenEditor { const se = this; switch (command) { + case 'html': + const saveoption = document.createElement('div'); + saveoption.classList.add('se-toolbar-option'); + saveoption.setAttribute('title', 'Design'); + saveoption.append(createSVG(DESIGNICON)); + + se.toolbar.classList.add('se-html'); + + saveoption.onclick = () => { + saveoption.remove(); + se.body.style.fontFamily = null; + + const fullscreenoption = se.toolbar.querySelector( + '#toolbar-fullscreen', + ); + if (fullscreenoption) { + const groupOptions = se.toolbar.querySelector('#group-options'); + groupOptions && groupOptions.append(fullscreenoption); + } + + se.toolbar.style.display = null; + se.toolbar.classList.remove('se-html'); + + const htmleditor = se.editor.querySelector('.se-html-editor'); + + if(htmleditor){ + se.setContent(htmleditor.value); + htmleditor.remove(); + } + + se.body.style.display = null; + }; + + se.toolbar.prepend(saveoption); + + const fullscreenoption = se.toolbar.querySelector( + '#toolbar-fullscreen', + ); + fullscreenoption && saveoption.after(fullscreenoption); + + se.toolbar.style.display = 'block'; + se.body.style.fontFamily = 'Courier'; + + const area = document.createElement('textarea'); + area.setAttribute('class', 'se-html-editor se-body'); + area.style = se.body.style; + area.style.minWidth = '100%'; + area.style.height = '100%'; + area.style.border = 'none'; + area.value = sourceFormatter.html(se.body.innerHTML); + + se.body.style.display = 'none'; + se.body.after(area); + + break; + case 'fullscreen': + const opt = se.toolbar.querySelector('#toolbar-fullscreen'); + if (opt.getAttribute('data-fullscreen')) { + if (se.editor.collapse) { + opt.innerHTML = ''; + opt.append(createSVG(EXPANDICON)); + + se.editor.collapse(); + } + + se.editor.style.maxHeight = null; + se.editor.style.height = null; + + se.body.style.maxHeight = null; + se.body.style.height = null; + + opt.removeAttribute('data-fullscreen'); + } else { + blowUpElement(se.editor, '#fff', e => { + opt.innerHTML = ''; + opt.append(createSVG(COLLAPSEICON)); + + se.editor.style.maxHeight = 'inherit'; + se.editor.style.height = '100%'; + + se.body.style.maxHeight = 'inherit'; + se.body.style.height = '100%'; + + opt.setAttribute('data-fullscreen', 'active'); + }); + } + break; case 'foreColor': case 'hiliteColor': const colorOption = se.toolbar.querySelector(`#toolbar-${command}`); @@ -2495,7 +2928,7 @@ export default class StrivenEditor { preview: true, palette: true, hue: true, - interaction: { hex: true, input: true }, + interaction: {hex: true, input: true}, }, }); @@ -2505,18 +2938,32 @@ export default class StrivenEditor { .toHEXA() .toString(); - pickr.hide(); + function execute() { + if (se.browser.isEdge() || se.browser.isFirefox()) { + document.execCommand(command, false, color); + } else { + document.execCommand(command, true, color); + } + } colorIcon.setAttribute('fill', color); - const refocus = se.body.onfocus; - se.body.onfocus = () => { - se.body.textContent && se.setRange(se.getRange()); - se.execFontStates(); - se.body.onfocus = refocus; + const enabler = () => { + execute(); + se.body.removeEventListener('keydown', enabler); }; - se.body.focus(); + if (se.range) { + if (se.range.collapsed) { + se.body.addEventListener('keydown', enabler); + } else { + execute(); + } + } else { + se.body.addEventListener('keydown', enabler); + } + + pickr.hide(); }); pickr.on('init', p => { @@ -2534,12 +2981,14 @@ export default class StrivenEditor { .getColor() .toHEXA() .toString(); - }); - pickr.on('show', p => { - const { app } = p['_root']; - se.setMenuOffset(colorOption, app); + se.body.focus(); }); + + // pickr.on('show', p => { + // const { app } = p['_root']; + // se.setMenuOffset(colorOption, app); + // }); } break; case 'insertOrderedList': @@ -2576,7 +3025,14 @@ export default class StrivenEditor { } else { this.openLinkMenu(); - setTimeout(() => this.linkMenu.querySelector('input').focus(), 100); + setTimeout(() => { + const selection = se.linkMenu.querySelector( + 'input[placeholder="Text content"]', + ); + selection && (selection.value = document.getSelection().toString()); + + this.linkMenu.querySelector('input').focus(); + }, 100); } break; case 'image': @@ -2613,13 +3069,11 @@ export default class StrivenEditor { } if (command === 'indent') { - [...this.body.getElementsByTagName('blockquote')] - .forEach(bq => { - const wrapper = document.createElement('div'); - wrapper.innerHTML = bq.outerHTML; - bq.replaceWith(wrapper); - }) + [...this.body.getElementsByTagName('blockquote')].forEach(bq => { + const wrapper = document.createElement('div'); + wrapper.innerHTML = bq.outerHTML; + bq.replaceWith(wrapper); + }); } - } }