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 eb2cf8bdbdf0193d1435fdb206fb7f9fa9d11637
parent 20213552d9069f414154302034ab3bf928622d04
Author: unitehenry <unitehenry@gmail.com>
Date:   Tue, 24 Mar 2020 15:43:39 -0400

indent fix

Diffstat:
Mpackage.json | 2+-
Msrc/defaults.js | 42+++++++++++++++++++++++++++---------------
Msrc/ko-striveneditor.js | 6++++--
Msrc/striveneditor.js | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
4 files changed, 115 insertions(+), 36 deletions(-)

diff --git a/package.json b/package.json @@ -1,7 +1,7 @@ { "name": "@striven-erp/striven-editor", "private": false, - "version": "3.1.5", + "version": "3.3.1", "description": "The lightweight zero-dependency Striven WYSIWYG editor.", "main": "./dist", "scripts": { diff --git a/src/defaults.js b/src/defaults.js @@ -124,20 +124,6 @@ const OPTIONGROUPS = { }, group: [ { - indent: { - viewBox: '0 0 1792 1792', - d: - 'M352 832q0 14-9 23l-288 288q-9 9-23 9-13 0-22.5-9.5t-9.5-22.5v-576q0-13 9.5-22.5t22.5-9.5q14 0 23 9l288 288q9 9 9 23zm1440 480v192q0 13-9.5 22.5t-22.5 9.5h-1728q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1728q13 0 22.5 9.5t9.5 22.5zm0-384v192q0 13-9.5 22.5t-22.5 9.5h-1088q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1088q13 0 22.5 9.5t9.5 22.5zm0-384v192q0 13-9.5 22.5t-22.5 9.5h-1088q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1088q13 0 22.5 9.5t9.5 22.5zm0-384v192q0 13-9.5 22.5t-22.5 9.5h-1728q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1728q13 0 22.5 9.5t9.5 22.5z', - }, - }, - { - outdent: { - viewBox: '0 0 1792 1792', - d: - 'M384 544v576q0 13-9.5 22.5t-22.5 9.5q-14 0-23-9l-288-288q-9-9-9-23t9-23l288-288q9-9 23-9 13 0 22.5 9.5t9.5 22.5zm1408 768v192q0 13-9.5 22.5t-22.5 9.5h-1728q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1728q13 0 22.5 9.5t9.5 22.5zm0-384v192q0 13-9.5 22.5t-22.5 9.5h-1088q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1088q13 0 22.5 9.5t9.5 22.5zm0-384v192q0 13-9.5 22.5t-22.5 9.5h-1088q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1088q13 0 22.5 9.5t9.5 22.5zm0-384v192q0 13-9.5 22.5t-22.5 9.5h-1728q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1728q13 0 22.5 9.5t9.5 22.5z', - }, - }, - { justifyLeft: { viewBox: '0 0 1792 1792', d: @@ -164,7 +150,7 @@ const OPTIONGROUPS = { menu: { viewBox: '0 0 1792 1792', d: - 'M1596 1385q0 117-79 196t-196 79q-135 0-235-100l-777-776q-113-115-113-271 0-159 110-270t269-111q158 0 273 113l605 606q10 10 10 22 0 16-30.5 46.5t-46.5 30.5q-13 0-23-10l-606-607q-79-77-181-77-106 0-179 75t-73 181q0 105 76 181l776 777q63 63 145 63 64 0 106-42t42-106q0-82-63-145l-581-581q-26-24-60-24-29 0-48 19t-19 48q0 32 25 59l410 410q10 10 10 22 0 16-31 47t-47 31q-12 0-22-10l-410-410q-63-61-63-149 0-82 57-139t139-57q88 0 149 63l581 581q100 98 100 235z', + 'M1088 1248v192q0 40-28 68t-68 28h-192q-40 0-68-28t-28-68v-192q0-40 28-68t68-28h192q40 0 68 28t28 68zm0-512v192q0 40-28 68t-68 28h-192q-40 0-68-28t-28-68v-192q0-40 28-68t68-28h192q40 0 68 28t28 68zm0-512v192q0 40-28 68t-68 28h-192q-40 0-68-28t-28-68v-192q0-40 28-68t68-28h192q40 0 68 28t28 68z', }, group: [ { @@ -175,6 +161,20 @@ const OPTIONGROUPS = { }, }, { + indent: { + viewBox: '0 0 1792 1792', + d: + 'M352 832q0 14-9 23l-288 288q-9 9-23 9-13 0-22.5-9.5t-9.5-22.5v-576q0-13 9.5-22.5t22.5-9.5q14 0 23 9l288 288q9 9 9 23zm1440 480v192q0 13-9.5 22.5t-22.5 9.5h-1728q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1728q13 0 22.5 9.5t9.5 22.5zm0-384v192q0 13-9.5 22.5t-22.5 9.5h-1088q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1088q13 0 22.5 9.5t9.5 22.5zm0-384v192q0 13-9.5 22.5t-22.5 9.5h-1088q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1088q13 0 22.5 9.5t9.5 22.5zm0-384v192q0 13-9.5 22.5t-22.5 9.5h-1728q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1728q13 0 22.5 9.5t9.5 22.5z', + }, + }, + { + outdent: { + viewBox: '0 0 1792 1792', + d: + 'M384 544v576q0 13-9.5 22.5t-22.5 9.5q-14 0-23-9l-288-288q-9-9-9-23t9-23l288-288q9-9 23-9 13 0 22.5 9.5t9.5 22.5zm1408 768v192q0 13-9.5 22.5t-22.5 9.5h-1728q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1728q13 0 22.5 9.5t9.5 22.5zm0-384v192q0 13-9.5 22.5t-22.5 9.5h-1088q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1088q13 0 22.5 9.5t9.5 22.5zm0-384v192q0 13-9.5 22.5t-22.5 9.5h-1088q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1088q13 0 22.5 9.5t9.5 22.5zm0-384v192q0 13-9.5 22.5t-22.5 9.5h-1728q-13 0-22.5-9.5t-9.5-22.5v-192q0-13 9.5-22.5t22.5-9.5h1728q13 0 22.5 9.5t9.5 22.5z', + }, + }, + { link: { viewBox: '0 0 1792 1792', d: @@ -225,11 +225,22 @@ const ACTIVEOPTIONCOLOR = '#777777'; const FONTNAMES = [ 'Segoe UI', 'Arial', + 'Verdana', 'Times New Roman', 'Sans Serif', 'Courier', ]; +const FONTSIZES = [ + '1 (8pt)', + '2 (10pt)', + '3 (12pt)', + '4 (14pt)', + '5 (18pt)', + '6 (24pt)', + '7 (36pt)', +]; + export { EXTENSIONS, FONTPACK, @@ -239,4 +250,5 @@ export { FORECOLORICON, HILITECOLORICON, FONTNAMES, + FONTSIZES, }; diff --git a/src/ko-striveneditor.js b/src/ko-striveneditor.js @@ -2,7 +2,7 @@ import StrivenEditor from './striveneditor'; export default class KoStrivenEditor { constructor(ko, bindingName = 'striveneditor') { - ko.bindingHandlers[bindingName] = { + ko.bindingHandlers.striveneditor = { init: function( element, valueAccessor, @@ -65,7 +65,9 @@ export default class KoStrivenEditor { // Set the editor in options options().api = editor; - return {controlsDescendantBindings: true}; + ko.applyBindings(bindingContext, editor.toolbar); + return { controlsDescendantBindings: true }; + }, }; } diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -8,11 +8,12 @@ import { FORECOLORICON, HILITECOLORICON, FONTNAMES, + FONTSIZES, } from './defaults.js'; // Helpers import denormalizeCamel from './denormalizeCamel'; -import computedStyleToInlineStyle from 'computed-style-to-inline-style'; +// import computedStyleToInlineStyle from 'computed-style-to-inline-style'; // Polyfills import ResizeObserver from 'resize-observer-polyfill'; @@ -29,15 +30,26 @@ export default class StrivenEditor { */ _bindContenteditableOnChange(el) { const se = this; + el.addEventListener('focus', function() { - el.data_orig = el.innerHTML; + if (el.data_orig === undefined) { + el.data_orig = el.innerHTML; + } }); el.addEventListener('blur', function() { - setTimeout(() => { - if (el.innerHTML != el.data_orig && document.activeElement != se.body) - se.options.change(se.getContent()); + if (el.innerHTML != el.data_orig && !se.toolbarClick) { + se.options.change(se.getContent()); delete el.data_orig; - }, 100); + } + }); + + se.toolbarClick = false; + se.toolbar.addEventListener('mousedown', () => { + se.toolbarClick = true; + }); + + se.toolbar.addEventListener('mouseup', () => { + se.toolbarClick = false; }); } @@ -257,7 +269,7 @@ export default class StrivenEditor { this.executeCommand(command); break; } - + optionElClick && optionElClick(); }; }); @@ -324,7 +336,9 @@ export default class StrivenEditor { this.toolbarOptionsGroup.classList.add('se-toolbar-options'); - toolbar.onclick = () => this.body.focus(); + toolbar.onclick = () => { + this.body.focus(); + }; // Append Font Options !this.options.minimal && this.initToolbarFontOptions(); @@ -341,8 +355,19 @@ export default class StrivenEditor { // toolbarMenuIcon.classList.add(this.options.fontPack); // toolbarMenuIcon.classList.add(this.optionGroups[group].menu); + const arrow = { + viewBox: '0 0 1792 1792', + d: + 'M1395 736q0 13-10 23l-466 466q-10 10-23 10t-23-10l-466-466q-10-10-10-23t10-23l50-50q10-10 23-10t23 10l393 393 393-393q10-10 23-10t23 10l50 50q10 10 10 23z', + }; + const svgSpan = this.constructSVG(this.optionGroups[group].menu); toolbarMenu.appendChild(svgSpan.getElementsByTagName('svg')[0]); + + const arrowSpan = this.constructSVG(arrow); + arrowSpan.classList.add('se-arrow-span'); + toolbarMenu.appendChild(arrowSpan); + this.toolbarOptionsGroup.appendChild(toolbarMenu); // add group to toolbarOptions @@ -446,6 +471,19 @@ export default class StrivenEditor { } }); + const attachmentOption = toolbar.querySelector('#toolbar-attachment'); + const removeFormatOption = toolbar.querySelector('#toolbar-removeFormat'); + + if (attachmentOption) { + attachmentOption.remove(); + this.toolbarOptionsGroup.append(attachmentOption); + } + + if (removeFormatOption) { + removeFormatOption.remove(); + this.toolbarOptionsGroup.append(removeFormatOption); + } + return toolbar; } @@ -525,7 +563,7 @@ export default class StrivenEditor { function initFontSizeMenu(select) { const menu = initMenu('fontSize'); - const sizes = [1, 2, 3, 4, 5, 6, 7]; + const sizes = FONTSIZES; sizes.forEach(s => { const fontOption = document.createElement('div'); @@ -601,11 +639,16 @@ 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 { + document.execCommand('removeFormat', true); + } + se.executeCommand('formatBlock', s.command); - computedStyleToInlineStyle(se.body, { - recursive: true, - properties: ['color'], - }); + se.execFontStates(); }; menu.append(fontOption); @@ -916,6 +959,10 @@ export default class StrivenEditor { body.onfocus = e => { !this.browser.isEdge() && this.setRange(); + if (!this.getContent()) { + this.setContent('&nbsp;'); + } + // disables all states this.options.toolbarOptions.forEach(opt => { if (typeof opt === 'string') { @@ -943,8 +990,8 @@ export default class StrivenEditor { this.execFontStates(); this.editor.classList.add('se-focus'); - - if(this.scrollPosition && !this.browser.isEdge()) { + + if (this.scrollPosition && !this.browser.isEdge()) { body.scrollTo(this.scrollPosition); } @@ -955,6 +1002,10 @@ export default class StrivenEditor { body.onblur = e => { this.editor.classList.remove('se-focus'); + if (this.getContent() === '&nbsp;') { + this.setContent(''); + } + this.scrollPosition = { y: body.scrollTop, x: body.scrollWidth, @@ -1559,7 +1610,7 @@ export default class StrivenEditor { function setResponsive() { let responsive = window.matchMedia('(max-width: 700px)').matches; - responsiveGroups(responsive || that.editor.offsetWidth < 700); + responsiveGroups(responsive || that.editor.offsetWidth < 900); } setResponsive(); @@ -2116,7 +2167,11 @@ export default class StrivenEditor { this.fontName && document.execCommand('fontName', false, this.fontName.textContent); this.fontSize && - document.execCommand('fontSize', false, this.fontSize.textContent); + document.execCommand( + 'fontSize', + false, + this.fontSize.textContent.split(' ')[0], + ); } else { this.foreColor && document.execCommand( @@ -2309,7 +2364,7 @@ export default class StrivenEditor { const isEmail = href ? href.includes('mailto') : false; const isLinkified = link.classList.contains('linkified') && - !se.validURL(link.textContent); + !se.validURL(href || link.textContent); if (href && (isEmail || isLinkified)) { link.outerHTML = link.textContent; @@ -2543,5 +2598,15 @@ export default class StrivenEditor { } break; } + + if(command === 'indent'){ + [...this.body.getElementsByTagName('blockquote')] + .forEach(bq => { + const wrapper = document.createElement('div'); + wrapper.innerHTML = bq.outerHTML; + bq.replaceWith(wrapper); + }) + } + } }