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 23127a9eef74b0400dc74171e6b3bbbe70737650
parent 4b41f028e3992a49f35bef6e464b339e924e08bf
Author: Eric Butler <ebutler@striven.com>
Date:   Fri, 14 Feb 2025 17:06:51 -0500

updates to upload image logic

Diffstat:
Msrc/defaults.js | 8+++++++-
Msrc/striveneditor.css | 17+++++++++++++++++
Msrc/striveneditor.js | 147++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
Msrc/utils.js | 26++++++++++++++++++++++++++
4 files changed, 143 insertions(+), 55 deletions(-)

diff --git a/src/defaults.js b/src/defaults.js @@ -237,6 +237,11 @@ const DESIGNICON = { 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' }; +const UPLOADICON = { + viewBox: '0 0 24 24', + d: 'M11 20H6.5Q4.22 20 2.61 18.43 1 16.85 1 14.58 1 12.63 2.17 11.1 3.35 9.57 5.25 9.15 5.88 6.85 7.75 5.43 9.63 4 12 4 14.93 4 16.96 6.04 19 8.07 19 11 20.73 11.2 21.86 12.5 23 13.78 23 15.5 23 17.38 21.69 18.69 20.38 20 18.5 20H13V12.85L14.6 14.4L16 13L12 9L8 13L9.4 14.4L11 12.85Z' +}; + export { EXTENSIONS, FONTPACK, @@ -249,5 +254,6 @@ export { FONTSIZES, COLLAPSEICON, EXPANDICON, - DESIGNICON + DESIGNICON, + UPLOADICON }; diff --git a/src/striveneditor.css b/src/striveneditor.css @@ -514,3 +514,20 @@ background-color: #d4d4d4; border-color: #8c8c8c; } + +/* Button tabs. Style button to look like a tab with rounded top borders */ +.striven-editor .se-tab-button { + background-color: #f1f1f1; + border: 1px solid #ccc; + border-bottom: none; + border-radius: 10px 10px 0 0; + padding: 5px; + cursor: pointer; + user-select: none; +} + +/* Active class for the active tab */ +.striven-editor .se-tab-button.tab-button-active { + background-color: #fff; + border-bottom: 1px solid #ccc; +} diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -11,11 +11,12 @@ import { FONTSIZES, COLLAPSEICON, EXPANDICON, - DESIGNICON + DESIGNICON, + UPLOADICON } from './defaults.js'; // Helpers -import { createSVG, denormalizeCamel, blowUpElement } from './utils'; +import { createSVG, denormalizeCamel, blowUpElement, getImageWidth, getImageDataURL } from './utils'; // Polyfills import ResizeObserver from 'resize-observer-polyfill'; @@ -922,15 +923,6 @@ export default class StrivenEditor { }); } - // Converty file to encoding - const convertImage = (file) => - new Promise((resolve, reject) => { - const reader = new FileReader(); - reader.readAsDataURL(file); - reader.onload = () => resolve(reader.result); - reader.onerror = (error) => reject(error); - }); - const afterPaste = () => { // After the paste setTimeout(() => { @@ -945,7 +937,7 @@ export default class StrivenEditor { if (e.clipboardData.files && e.clipboardData.files.length > 0 && e.clipboardData.files[0].type.includes('image')) { e.preventDefault(); - convertImage(e.clipboardData.files[0]) + getImageDataURL(e.clipboardData.files[0]) .then((res) => { if (se.options.imageUrl) { se.getImage(res) @@ -1354,10 +1346,17 @@ export default class StrivenEditor { const imageMenu = document.createElement('div'); const imageMenuHeader = document.createElement('div'); + // Upload tab button const imageMenuUploadTabButton = document.createElement('button'); + imageMenuUploadTabButton.classList.add('se-tab-button', 'se-tab-button-upload', 'tab-button-active'); + // Link tab button const imageMenuLinkTabButton = document.createElement('button'); + imageMenuLinkTabButton.classList.add('se-tab-button', 'se-tab-button-link'); + // Image Menu Tabs const imageMenuUploadTab = document.createElement('div'); + imageMenuUploadTab.classList.add('se-image-menu-tab', 'se-image-menu-upload-tab'); + const imageMenuLinkTab = document.createElement('div'); imageMenuUploadTab.classList.add('se-image-menu-tab', 'se-image-menu-link-tab'); imageMenuLinkTab.style.display = 'none'; @@ -1375,7 +1374,7 @@ export default class StrivenEditor { const imageMenuUploadDropZoneText = document.createElement('p'); imageMenuUploadDropZoneText.textContent = 'Click to upload OR drag and drop images here'; - imageMenuUploadDropZone.appendChild(createSVG(DESIGNICON, undefined, '48px', '48px')); + imageMenuUploadDropZone.appendChild(createSVG(UPLOADICON, undefined, '48px', '48px')); imageMenuUploadDropZone.appendChild(imageMenuUploadDropZoneText); // Link form inputs @@ -1467,48 +1466,79 @@ export default class StrivenEditor { // Process files const processFiles = function (files) { - for (const i of files) { + return new Promise((resolve) => { + // Create an image tag string with a width of 90% the se.body element if it is wider than the body, but not to exceed a width of 1200px + const bodyWidth = se.body.offsetWidth; + const processPromises = []; + + for (const file of files) { + // Check if the file is an image + if (!file.type.includes('image')) { + continue; + } - // Check if the file is an image - if (!i.type.includes('image')) { - continue; - } + const processPromise = new Promise((processPromiseResolve) => { + getImageDataURL(file).then((dataUrl) => { + // Get the width of the image + getImageWidth(dataUrl).then((width) => { + // If the image is wider than the body, set the width to 90% of the body width + if (width > bodyWidth) { + // Set the width to 90% + width = bodyWidth * 0.9; + } - const reader = new FileReader(); - reader.onload = (e) => { - const dataUrl = e.target.result; + if (width > 1200) { + width = 1200; + } - if (se.browser.isFirefox() || se.browser.isEdge()) { - document.execCommand('insertImage', false, dataUrl); - } else { - document.execCommand('insertImage', true, dataUrl); - } - }; + // Create the image tag to insert + const imgTag = `<img src="${dataUrl}" width="${width}px" alt=""/>`; - reader.readAsDataURL(i); - } + if (se.browser.isFirefox() || se.browser.isEdge()) { + document.execCommand('insertHTML', false, imgTag); + } else { + document.execCommand('insertHTML', true, imgTag); + } + + processPromiseResolve(); + }); + }); + }); + + processPromises.push(processPromise); + } + + // Resolve the promise when all the images have been processed + Promise.all(processPromises).then(() => { + resolve(); + }); + }); }; // Register click handlers - let activeTab = 'upload'; + // Create an event handler for upload tab click that will show the upload tab imageMenuUploadTabButton.onclick = (e) => { - // Set the active tab to the upload tab - activeTab = 'upload'; // Hide the link tab imageMenuLinkTab.style.display = 'none'; // Show the upload tab imageMenuUploadTab.style.display = 'block'; + // Make this tab active + imageMenuUploadTabButton.classList.add('tab-button-active'); + // Remove the active class from the link tab + imageMenuLinkTabButton.classList.remove('tab-button-active'); }; // Create an event handler for the link tab click that will show the link tab imageMenuLinkTabButton.onclick = (e) => { - // Set the active tab to the link tab - activeTab = 'link'; // Hide the upload tab imageMenuUploadTab.style.display = 'none'; // Show the link tab imageMenuLinkTab.style.display = 'block'; + // Make this tab active + imageMenuLinkTabButton.classList.add('tab-button-active'); + // Remove the active class from the upload tab + imageMenuUploadTabButton.classList.remove('tab-button-active'); }; // Create an event handler for when the images are selected @@ -1522,18 +1552,18 @@ export default class StrivenEditor { const files = e.target.files; // For each file, get the data url and insert the image - processFiles(files); - - // Clean up - se.closeImageMenu(); - se.makeImagesClickable(); - // Clear the inputs - clearImageMenuInputs(); + processFiles(files).then(() => { + // Clean up + se.closeImageMenu(); + se.makeImagesClickable(); + // Clear the inputs + clearImageMenuInputs(); + }); }; // When clicking on the dropzone, trigger the file input imageMenuUploadDropZone.onclick = (e) => { - se.body.focus(); + se.body.focus(); // Trigger the file input imageMenuUploadInput.click(); }; @@ -1552,13 +1582,15 @@ export default class StrivenEditor { imageMenuUploadDropZone.ondrop = (e) => { e.preventDefault(); imageMenuUploadDropZone.classList.remove('dropzone-hot'); - // Process the files - processFiles(e.dataTransfer.files); - // Clean up - se.closeImageMenu(); - se.makeImagesClickable(); - // Clear the inputs - clearImageMenuInputs(); + + // For each file, get the data url and insert the image + processFiles(e.dataTransfer.files).then(() => { + // Clean up + se.closeImageMenu(); + se.makeImagesClickable(); + // Clear the inputs + clearImageMenuInputs(); + }); }; // Insert image button click event @@ -1622,9 +1654,6 @@ export default class StrivenEditor { clearImageMenuInputs(); }; - imageMenuHeader.classList.add('se-popup-header'); - //imageMenuHeader.innerText = 'Insert Image'; - // Set the tab text imageMenuUploadTabButton.textContent = 'Upload'; imageMenuLinkTabButton.textContent = 'Image URL'; @@ -2428,13 +2457,23 @@ export default class StrivenEditor { /** * Opens the editor's image menu popup */ - openImageMenu() { + openImageMenu(isEdit) { const se = this; se.closeAllMenus(); se.setMenuOffset(se.toolbar.querySelector('#toolbar-image'), se.imageMenu); + // Select the tab buttons + const imageMenuUploadTabButton = se.imageMenu.querySelector('.se-tab-button-upload'); + const imageMenuLinkTabButton = se.imageMenu.querySelector('.se-tab-button-link'); + + if (isEdit) { + imageMenuLinkTabButton.click(); + } else { + imageMenuUploadTabButton.click(); + } + // Clear the input fields const imageInputs = se.imageMenu.querySelectorAll('input'); for (const imageInput of imageInputs) { @@ -3008,7 +3047,7 @@ export default class StrivenEditor { editImageMenu.remove(); image.classList.add('se-image-to-edit'); - se.openImageMenu(); + se.openImageMenu(true); const linkInputs = se.imageMenu.querySelectorAll('input'); linkInputs[0].value = image.getAttribute('src'); diff --git a/src/utils.js b/src/utils.js @@ -89,3 +89,29 @@ export const blowUpElement = (el, bg = '#fff', onExpand) => { window.addEventListener('keydown', escapeEvt); el.addEventListener('keydown', escapeEvt); }; + +/** + * Gets the width of an image + * @param {string} url + * @returns + */ +export const getImageWidth = (url) => { + return new Promise((resolve) => { + const img = new Image(); + img.onload = () => resolve(img.width); + img.src = url; + }); +}; + +/** + * Gets the data URL of an image + * @param {File} file + * @returns + */ +export const getImageDataURL = (file) => + new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.readAsDataURL(file); + reader.onload = () => resolve(reader.result); + reader.onerror = (error) => reject(error); + });