commit dd8f77e334d6bfee66e5b4ffa532cd23c29e2d77
parent c2711c494a9bbf6b07689e64775d53416613ab54
Author: unitehenry <unitehenry@gmail.com>
Date: Wed, 18 Mar 2020 14:00:48 -0400
computed styles format
Diffstat:
5 files changed, 130 insertions(+), 55 deletions(-)
diff --git a/package-lock.json b/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "@striven-erp/striven-editor",
- "version": "2.9.994",
+ "version": "3.1.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@@ -2006,6 +2006,11 @@
"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",
diff --git a/package.json b/package.json
@@ -1,7 +1,7 @@
{
"name": "@striven-erp/striven-editor",
"private": false,
- "version": "3.0.0",
+ "version": "3.1.1",
"description": "The lightweight zero-dependency Striven WYSIWYG editor.",
"main": "./dist",
"scripts": {
@@ -56,6 +56,7 @@
},
"dependencies": {
"@simonwep/pickr": "^1.5.0",
+ "computed-style-to-inline-style": "^3.0.0",
"linkifyjs": "^2.1.9"
}
}
diff --git a/src/defaults.js b/src/defaults.js
@@ -199,6 +199,7 @@ const OPTIONGROUPS = {
const DEFAULTOPTIONS = [
'fontName',
'fontSize',
+ 'fontFormat',
'foreColor',
'hiliteColor',
'bold',
diff --git a/src/ko-striveneditor.js b/src/ko-striveneditor.js
@@ -1,64 +1,72 @@
import StrivenEditor from './striveneditor';
export default class KoStrivenEditor {
- constructor(ko, bindingName = 'striveneditor') {
- ko.bindingHandlers[bindingName] = {
- init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
- // Get options
- let options = valueAccessor();
- // Options must be passed as an observable
- if (!ko.isObservable(options)) {
- throw "Options must be an observable.";
- }
+ constructor(ko, bindingName = 'striveneditor') {
+ ko.bindingHandlers[bindingName] = {
+ init: function(
+ element,
+ valueAccessor,
+ allBindings,
+ viewModel,
+ bindingContext,
+ ) {
+ // Get options
+ let options = valueAccessor();
+ // Options must be passed as an observable
+ if (!ko.isObservable(options)) {
+ throw 'Options must be an observable.';
+ }
- // Get the value binding in case we are binding the editor's contents to an observable
- let value = allBindings().value;
- if (value && !ko.isObservable(value)) {
- throw "Value must be an observable.";
- }
+ // Get the value binding in case we are binding the editor's contents to an observable
+ let value = allBindings().value;
+ if (value && !ko.isObservable(value)) {
+ throw 'Value must be an observable.';
+ }
- // Pass the value to the editor in options
- options().value = value();
+ // Pass the value to the editor in options
+ options().value = value();
- let onChange = null;
- let changeOpt = options().change
- if (changeOpt) {
- if (ko.isObservable(changeOpt)) {
- onChange = (val) => changeOpt(val);
- } else {
- onChange = changeOpt;
- }
- }
+ let onChange = null;
+ let changeOpt = options().change;
+ if (changeOpt) {
+ if (ko.isObservable(changeOpt)) {
+ onChange = val => changeOpt(val);
+ } else {
+ onChange = changeOpt;
+ }
+ }
- let editor = new StrivenEditor(element, {
- ...options(),
- change: onChange,
- toolbarTemplate: (options().toolbarTemplate && options().toolbarTemplate())
- });
- let pauseUpdate = false;
+ let editor = new StrivenEditor(element, {
+ ...options(),
+ change: onChange,
+ toolbarTemplate:
+ options().toolbarTemplate && options().toolbarTemplate(),
+ });
+ let pauseUpdate = false;
- // Handle updates and changes to value observable
- if (ko.isObservable(value)) {
- // Listen to input event
- editor.body.oninput = e => {
- pauseUpdate = true;
- value(editor.getContent());
- pauseUpdate = false;
- };
+ // Handle updates and changes to value observable
+ if (ko.isObservable(value)) {
+ // Listen to input event
+ editor.body.oninput = e => {
+ pauseUpdate = true;
+ value(editor.getContent());
+ pauseUpdate = false;
+ };
- // Subscribe to the value observable to detect when it is changed and update the editor
- value.subscribe((newValue) => {
- // Update the content if it's not being updated from within the editor
- if (!pauseUpdate) {
- editor.setContent(newValue);
- }
- });
- }
+ // Subscribe to the value observable to detect when it is changed and update the editor
+ value.subscribe(newValue => {
+ // Update the content if it's not being updated from within the editor
+ if (!pauseUpdate) {
+ editor.setContent(newValue);
+ }
+ });
+ }
- // Set the editor in options
- options().api = editor;
+ // Set the editor in options
+ options().api = editor;
- }
- };
- }
+ return {controlsDescendantBindings: true};
+ },
+ };
+ }
}
diff --git a/src/striveneditor.js b/src/striveneditor.js
@@ -12,6 +12,7 @@ import {
// Helpers
import denormalizeCamel from './denormalizeCamel';
+import computedStyleToInlineStyle from 'computed-style-to-inline-style';
// Polyfills
import ResizeObserver from 'resize-observer-polyfill';
@@ -552,6 +553,41 @@ export default class StrivenEditor {
return menu;
}
+ function initFontFormatMenu() {
+ const menu = initMenu('fontFormat');
+
+ const formats = [
+ {command: 'H1', option: '<h1 style="margin: 0; color: #000;">Heading 1</h1>'},
+ {command: 'H2', option: '<h2 style="margin: 0; color: #000;">Heading 2</h2>'},
+ {command: 'H3', option: '<h3 style="margin: 0; color: #000;">Heading 3</h4>'},
+ {command: 'H4', option: '<h4 style="margin: 0; color: #000;">Heading 4</h4>'},
+ {command: 'H5', option: '<h5 style="margin: 0; color: #000;">Heading 5</h5>'},
+ {command: 'H6', option: '<h6 style="margin: 0; color: #000;">Heading 6</h6>'},
+ {command: 'P', option: '<p style="margin: 0; color: #000;">Paragraph</p>'},
+ ];
+
+ formats.forEach(s => {
+ const fontOption = document.createElement('div');
+
+ fontOption.classList.add('se-toolbar-popup-option');
+ fontOption.innerHTML = s.option;
+
+ fontOption.onclick = e => {
+ menu.close();
+ se.body.focus();
+ se.executeCommand('formatBlock', s.command);
+ computedStyleToInlineStyle(se.body, {
+ recursive: true,
+ properties: ['color'],
+ });
+ };
+
+ menu.append(fontOption);
+ });
+
+ return menu;
+ }
+
// Get enabled toolbar options
const enabledOptions = this.options.toolbarOptions;
@@ -612,6 +648,30 @@ export default class StrivenEditor {
this.toolbarOptionsGroup.append(fontSizeSelect);
this.fontSize = selectedFontSize;
}
+
+ if (enabledOptions.includes('fontFormat')) {
+ const fontFormatSelect = document.createElement('div');
+ const selectedFontFormat = document.createElement('p');
+ const menu = initFontFormatMenu();
+
+ fontFormatSelect.onclick = () => {
+ se.range = se.getRange();
+ if (menu.dataset.active === 'true') {
+ menu.close();
+ } else {
+ menu.open();
+ }
+ };
+
+ fontFormatSelect.setAttribute('id', 'toolbar-fontFormat');
+ selectedFontFormat.textContent = 'Format';
+
+ fontFormatSelect.classList.add('se-toolbar-selection');
+ selectedFontFormat.classList.add('se-toolbar-option');
+
+ fontFormatSelect.append(selectedFontFormat);
+ this.toolbarOptionsGroup.append(fontFormatSelect);
+ }
}
/**
@@ -2351,7 +2411,7 @@ export default class StrivenEditor {
pickr.hide();
colorIcon.setAttribute('fill', color);
-
+
const refocus = se.body.onfocus;
se.body.onfocus = () => {
se.body.textContent && se.setRange(se.getRange());