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 6da8bb6ff88ca4725629be1cb9d01c72eb9f1f6b
parent 118f87ceeb0fc2ea17fb1ee4530343318ffe6ae2
Author: Henry Unite <unitehenry@gmail.com>
Date:   Mon,  7 Sep 2020 15:29:04 -0400

consolidate utilities

Diffstat:
Dsrc/blowupelement.js | 52----------------------------------------------------
Dsrc/createSVG.js | 20--------------------
Dsrc/denormalizeCamel.js | 11-----------
Msrc/striveneditor.js | 8+++++---
Asrc/utils.js | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 97 insertions(+), 86 deletions(-)

diff --git a/src/blowupelement.js b/src/blowupelement.js @@ -1,52 +0,0 @@ -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(); - - const escapeEvt = (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; - } - } - } - - window.addEventListener('keydown', escapeEvt); - el.addEventListener('keydown', escapeEvt); -} diff --git a/src/createSVG.js b/src/createSVG.js @@ -1,20 +0,0 @@ -/* - * 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/denormalizeCamel.js b/src/denormalizeCamel.js @@ -1,11 +0,0 @@ -/** - * Converts camel case string to a readable sentence - * @param {String} Camel case string to convert - * @returns {String} Readable sentence - */ -export default (text) => { - const result = text.replace( /([A-Z])/g, " $1" ); - const finalResult = result.charAt(0).toUpperCase() + result.slice(1); - - return finalResult; -} diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -15,9 +15,11 @@ import { } from './defaults.js'; // Helpers -import createSVG from './createSVG'; -import denormalizeCamel from './denormalizeCamel'; -import blowUpElement from './blowupelement'; +import { + createSVG, + denormalizeCamel, + blowUpElement +} from './utils'; // Polyfills import ResizeObserver from 'resize-observer-polyfill'; diff --git a/src/utils.js b/src/utils.js @@ -0,0 +1,91 @@ +/** + * 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 const createSVG = () => { + 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'); +} + +/** + * Converts camel case string to a readable sentence + * @param {String} text camel case string to convert + * @returns {String} Readable sentence + */ +export const denormalizeCamel = (text) => { + const result = text.replace( /([A-Z])/g, " $1" ); + const finalResult = result.charAt(0).toUpperCase() + result.slice(1); + + return finalResult; +} + +/** + * Expand DOM element + * @param {HTMLElement} el Element to expand + * @param {String} bg Background color of expanded content + * @param {Function} onExpand Handler function called when element is expanded + */ +export const 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(); + + const escapeEvt = (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; + } + } + } + + window.addEventListener('keydown', escapeEvt); + el.addEventListener('keydown', escapeEvt); +} +\ No newline at end of file