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 0ad606fac1bce29d6caff5582e4cc5ee1ec3eb5e
parent a486c6219fddf446cae269f7a84759a95d99df36
Author: unitehenry <unitehenry@gmail.com>
Date:   Wed,  6 Nov 2019 10:19:55 -0500

custom options minimal mode

Diffstat:
Mdemo/src/App.vue | 11+++++++++++
Mpackage.json | 2+-
Msrc/striveneditor.css | 5+++++
Msrc/striveneditor.js | 91+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
Msrc/vue-striveneditor.vue | 1+
5 files changed, 72 insertions(+), 38 deletions(-)

diff --git a/demo/src/App.vue b/demo/src/App.vue @@ -8,6 +8,7 @@ :toolbar-hide="toolbarHide" :minimal="minimal" :toolbar-bottom="toolbarBottom" + :toolbar-options="toolbarOptions" :placeholder="'Type something in here...'" style="min-height: 300px; width: 800px;" :meta-url="metaUrl" @@ -39,6 +40,16 @@ export default { onInvalidFile() { alert("invalid file"); }, + toolbarOptions: [ + { + icon: { + viewBox: "0 0 1792 1792", + d: + "M1600 736v192q0 40-28 68t-68 28h-416v416q0 40-28 68t-68 28h-192q-40 0-68-28t-28-68v-416h-416q-40 0-68-28t-28-68v-192q0-40 28-68t68-28h416v-416q0-40 28-68t68-28h192q40 0 68 28t28 68v416h416q40 0 68 28t28 68z" + }, + handler: () => console.log('hello') + } + ], placeholder: "Begin typing in this editor...", submitOnEnter: e => { if (e.ctrlKey) { diff --git a/package.json b/package.json @@ -1,7 +1,7 @@ { "name": "@striven-erp/striven-editor", "private": false, - "version": "2.4.3", + "version": "2.4.5", "description": "The lightweight Striven WYSIWYG editor built with pure JavaScript.", "main": "./dist", "scripts": { diff --git a/src/striveneditor.css b/src/striveneditor.css @@ -84,6 +84,11 @@ user-select: none; } +.striven-editor .se-toolbar.option svg { + width: 16px; + height: 14px; +} + .striven-editor .se-toolbar-option.se-toolbar-option-active { border: 1px solid rgba(0, 0, 0, .25); background-color: #d4e7fc; diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -92,6 +92,19 @@ export default class StrivenEditor { options.toolbarOptions || (this.options.toolbarOptions = DEFAULTOPTIONS); options.activeOptionColor || (this.options.activeOptionColor = ACTIVEOPTIONCOLOR); (options.fileUpload !== false) && (this.options.fileUpload = true); + + if (options.toolbarOptions && options.minimal) { + const customs = options.toolbarOptions.filter(opt => typeof opt === 'object'); + this.options.toolbarOptions = [ + 'bold', + 'italic', + 'underline', + 'insertUnorderedList', + 'attachment', + 'link', + ...customs + ] + } } else { this.options = { fontPack: FONTPACK, @@ -284,34 +297,32 @@ export default class StrivenEditor { // toolbar group for custom options const customOptions = this.options.toolbarOptions.filter(option => typeof option === "object"); if (customOptions.length > 0) { - this.customToolbarMenu = document.createElement("div"); - - this.customToolbarMenu.classList.add("se-toolbar-menu"); - this.customToolbarMenu.id = `menu-custom`; + customOptions.forEach(opt => { + const { icon, handler } = opt; - const customSVGViewBox = "0 0 1792 1792"; - const customSVGD = "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"; + this.customOptionsGroup = document.createElement('div'); + this.customOptionsGroup.classList.add('se-toolbar-group'); - this.customToolbarMenu.appendChild(this.constructSVG({ viewBox: customSVGViewBox, d: customSVGD })); - this.toolbarOptionsGroup.appendChild(this.customToolbarMenu); + if (typeof icon === 'object') { + const option = this.constructSVG({ viewBox: icon.viewBox, d: icon.d }); + option.classList.add('se-toolbar-option'); - this.customToolbarGroup = document.createElement("div"); + option.onclick = () => handler(); - this.customToolbarGroup.classList.add("se-toolbar-group"); - this.customToolbarGroup.id = "group-custom"; + this.customOptionsGroup.appendChild(option); + } else { + const option = document.createElement('img'); + option.setAttribute('src', opt.icon); + option.setAttribute('alt', 'custom option'); + option.classList.add('se-toolbar-option'); - customOptions.forEach((customOption) => { - const { icon, handler } = customOption; - const optionSpan = this.constructSVG(icon); + option.onclick = () => handler(); - optionSpan.id = `toolbar-customOption`; - optionSpan.style.margin = "0 10px"; - optionSpan.onclick = () => handler(); + this.customOptionsGroup.appendChild(option); + } - this.customToolbarGroup.appendChild(optionSpan); + this.toolbarOptionsGroup.appendChild(this.customOptionsGroup); }) - - this.toolbarOptionsGroup.appendChild(this.customToolbarGroup); } toolbar.appendChild(this.toolbarOptionsGroup); @@ -339,7 +350,7 @@ export default class StrivenEditor { this.toolbarGroups.forEach((group) => { if (group && group.children.length < 1) { const groupName = group.id.split("-")[1]; - const menu = this.toolbarMenus.filter((menu) => menu.id.split("-")[1] === groupName)[0]; + const menu = this.toolbarMenus.filter((menu) => (menu && menu.id.split("-")[1] === groupName))[0]; menu.remove(); } }) @@ -476,9 +487,9 @@ export default class StrivenEditor { } const bodyFocus = body.onfocus; - body.onfocus = () => { + body.onfocus = (e) => { !this.browser.isEdge() && this.setRange(); - + this.getActiveOptions().forEach(opt => { !document.queryCommandState(opt) && this.executeCommand(opt); }) @@ -951,14 +962,18 @@ export default class StrivenEditor { responsiveMinimal(responsive); } + function hideOption(option) { + const optionEl = that.toolbar.querySelector(`#toolbar-${option}`); + optionEl && (optionEl.style.display = 'none'); + } + this.toolbarMenus.forEach(menu => (menu && (menu.style.display = "none"))); - this.toolbar.querySelector("#toolbar-strikethrough").style.display = "none"; - this.toolbar.querySelector("#toolbar-image").style.display = "none"; - this.toolbar.querySelector("#toolbar-insertOrderedList").style.display = - "none"; - this.toolbar.querySelector("#group-textAlign").style.display = "none"; - this.toolbar.querySelector("#toolbar-removeFormat").style.display = "none"; + hideOption('strikethrough'); + hideOption('image'); + hideOption('insertOrderedList'); + hideOption('textAlign'); + hideOption('removeFormat'); if (this.editor.offsetWidth < 300) { responsiveMinimal(true); @@ -1220,14 +1235,16 @@ export default class StrivenEditor { toolbarState() { this.options.toolbarOptions.forEach(option => { - const toolbarOption = this.toolbar.querySelector(`#toolbar-${option}`); - if (!option.toLowerCase().includes('justify') && !option.toLowerCase().includes('list')) { - if (document.queryCommandState(option)) { - toolbarOption.classList.add('se-toolbar-option-active'); - // toolbarOption.querySelector('path').setAttribute('fill', this.options.activeOptionColor); - } else { - toolbarOption.classList.remove('se-toolbar-option-active'); - // toolbarOption.querySelector('path').setAttribute('fill', '#333'); + if(typeof option === 'string') { + const toolbarOption = this.toolbar.querySelector(`#toolbar-${option}`); + if (!option.toLowerCase().includes('justify') && !option.toLowerCase().includes('list')) { + if (document.queryCommandState(option)) { + toolbarOption.classList.add('se-toolbar-option-active'); + // toolbarOption.querySelector('path').setAttribute('fill', this.options.activeOptionColor); + } else { + toolbarOption.classList.remove('se-toolbar-option-active'); + // toolbarOption.querySelector('path').setAttribute('fill', '#333'); + } } } }) diff --git a/src/vue-striveneditor.vue b/src/vue-striveneditor.vue @@ -10,6 +10,7 @@ export default { props: { toolbarHide: Boolean, toolbarBottom: Boolean, + toolbarOptions: Array, minimal: Boolean, metaUrl: String, extensions: Array,