commit d0cc3db305bd0cba364cc7ed2551f34ae870498c
parent 76f408203b871fdf7ccf231782666a58058e3007
Author: Eric Butler <ebutler@striven.com>
Date: Mon, 10 Mar 2025 11:52:42 -0400
Merge branch 'dev'
Diffstat:
4 files changed, 119 insertions(+), 115 deletions(-)
diff --git a/package.json b/package.json
@@ -1,7 +1,7 @@
{
"name": "@striven-erp/striven-editor",
"private": false,
- "version": "3.19.10",
+ "version": "3.19.11",
"description": "The editor used for the Striven ERP system.",
"main": "./dist",
"scripts": {
diff --git a/src/striveneditor.css b/src/striveneditor.css
@@ -197,18 +197,20 @@
fill: #777 !important;
}
-.striven-editor .se-image-uploading {
- width: 100%; height: 100%; background-color: #eee;display: flex; align-items: center; justify-content: center;
-}
-.striven-editor .se-image-uploading::after {
- content: "";
- display: block;
- width: 100px; /* Adjust as needed */
- height: 100px; /* Adjust as needed */
+.striven-editor.se-image-uploading::after,
+.striven-editor.se-image-uploading:empty::after {
+ content: '';
+ position: absolute;
+ top: 0;
+ left: 0;
+ right: 0;
+ bottom: 0;
+ background-color: rgba(255, 255, 255, 0.8);
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Ctitle%3Ecloud-arrow-up-outline%3C/title%3E%3Cg %3E%3Cpath fill='%23666' d='M4.03 12.03C3.34 12.71 3 13.53 3 14.5S3.34 16.29 4.03 17C4.71 17.66 5.53 18 6.5 18H13.09C13.04 18.33 13 18.66 13 19C13 19.34 13.04 19.67 13.09 20H6.5C5 20 3.69 19.5 2.61 18.43C1.54 17.38 1 16.09 1 14.58C1 13.28 1.39 12.12 2.17 11.1S4 9.43 5.25 9.15C5.67 7.62 6.5 6.38 7.75 5.43S10.42 4 12 4C13.95 4 15.6 4.68 16.96 6.04C18.32 7.4 19 9.05 19 11C20.15 11.13 21.1 11.63 21.86 12.5C22.37 13.07 22.7 13.71 22.86 14.42C21.82 13.54 20.5 13 19 13C18.89 13 18.79 13 18.68 13C18.62 13 18.56 13 18.5 13H17V11C17 9.62 16.5 8.44 15.54 7.46C14.56 6.5 13.38 6 12 6S9.44 6.5 8.46 7.46C7.5 8.44 7 9.62 7 11H6.5C5.53 11 4.71 11.34 4.03 12.03M16 18H18V22H20V18H22L19 15L16 18Z' /%3E%3Canimate attributeName='opacity' values='0.2; 1; 0.2' dur='1.5s' repeatCount='indefinite' /%3E%3C/g%3E%3C/svg%3E");
- background-size: cover; /* Adjust as needed */
- background-repeat: no-repeat; /* Adjust as needed */
+ background-size: 128px;
+ background-position: center;
+ background-repeat: no-repeat;
}
/** Toolbar **/
diff --git a/src/striveneditor.js b/src/striveneditor.js
@@ -16,7 +16,7 @@ import {
} from './defaults.js';
// Helpers
-import { createSVG, denormalizeCamel, blowUpElement, getImageDimensions, getImageDataURL, computeImageDimensions } from './utils';
+import { createSVG, denormalizeCamel, blowUpElement, getImageDimensions, getImageDataURL, computeImageDimensions, createImageElement } from './utils';
// Polyfills
import ResizeObserver from 'resize-observer-polyfill';
@@ -922,10 +922,10 @@ export default class StrivenEditor {
};
// Paste image logic
- if (e.clipboardData.files && e.clipboardData.files.length > 0 && e.clipboardData.files[0].type.includes('image')) {
+ if (e.clipboardData.files && e.clipboardData.files.length > 0 ) {
e.preventDefault();
-
- se.insertImage(e.clipboardData.files[0]).finally(() => {
+
+ se.insertImages(e.clipboardData.files).finally(() => {
afterPaste();
});
@@ -1433,39 +1433,6 @@ export default class StrivenEditor {
imageMenuUploadInput.value = '';
};
- // Process files
- const processFiles = function (files) {
- return new Promise((resolve) => {
- // Create a promise chain to insert each image in the files array only after the previous one is completed.
- // p = Promise.resolve() is used to start the chain with a resolved promise.
- let p = Promise.resolve();
- for (let i = 0; i < files.length; i++) {
- p = p.then(() => se.insertImage(files[i]));
- }
-
- // Add the resolve to the end of the promise chain
- p.then(() => resolve());
-
- //const processPromises = [];
-
- // for (const file of files) {
- // // Check if the file is an image
- // if (!file.type.includes('image')) {
- // continue;
- // }
-
- // const processPromise = se.insertImage(file);
-
- // processPromises.push(processPromise);
- // }
-
- // // Resolve the promise when all the images have been processed
- // Promise.all(processPromises).then(() => {
- // resolve();
- // });
- });
- };
-
// Register click handlers
// Create an event handler for upload tab click that will show the upload tab
@@ -1498,13 +1465,14 @@ export default class StrivenEditor {
// Create an event handler for when the images are selected
imageMenuUploadInput.onchange = (e) => {
+ // Clean up
+ se.closeImageMenu();
+
// Get the files that were selected
const files = e.target.files;
// For each file, get the data url and insert the image
- processFiles(files).then(() => {
- // Clean up
- se.closeImageMenu();
+ se.insertImages(files).then(() => {
// Clear the inputs
clearImageMenuInputs();
});
@@ -1535,10 +1503,12 @@ export default class StrivenEditor {
imageMenuUploadDropZone.classList.remove('dropzone-hot');
+ // Clean up
+ se.closeImageMenu();
+
+ const files = e.dataTransfer.files;
// For each file, get the data url and insert the image
- processFiles(e.dataTransfer.files).then(() => {
- // Clean up
- se.closeImageMenu();
+ se.insertImages(files).then(() => {
// Clear the inputs
clearImageMenuInputs();
});
@@ -3430,43 +3400,70 @@ export default class StrivenEditor {
}
/**
- * Inserts image into the editor, uploads to the server if imageUrl is provided otherwise uses data:image url
- * @param {*} file
+ * Inserts a node into the editor
+ * @param {*} node
+ * @param {*} range
+ */
+ insertNode(node, range) {
+ const se = this;
+ // Ensre that any selected contents is deleted before inserting
+ range.deleteContents();
+ range.insertNode(node);
+ range.setStartAfter(node);
+
+ se.setRange(range);
+ }
+
+ /**
+ * Inserts images into the editor
+ * @param {*} files
* @returns
*/
- insertImage(file) {
+ insertImages(files) {
const se = this;
- se.body.focus();
- se.setRange();
- return new Promise((resolveImageInsert, reject) => {
- const bodyWidth = se.body.offsetWidth;
+ const currentRange = se.getRange();
- const showUI = !(se.browser.isFirefox() || se.browser.isEdge());
+ return new Promise((resolve) => {
+ // Add busy class to striven editor
+ se.editor.classList.add('se-image-uploading');
se._isImageUploading = true;
- // Generate a random string to use as the image name
- //const randomString = 'image-' + Math.random().toString(36);
- let currentRange = se.getRange();
- // Insert image with a temporary src
- //document.execCommand('insertHTML', showUI, `<div id="${randomString}" class="se-image-uploading" ></div>`);
- //const tempImg = document.getElementById(randomString);
- const tempImg = document.createElement('div');
- //tempImg.id = randomString;
- tempImg.classList.add('se-image-uploading');
- // Ensre that any selected contents is deleted before inserting
- currentRange.deleteContents();
- currentRange.insertNode(tempImg);
- currentRange.setStartAfter(tempImg);
- this.range.collapse(true);
-
- se.setRange(currentRange);
-
- if (!tempImg) {
- const err = 'Failed to insert image into the DOM. Make sure the editor body is focused and try again.';
-
- reject(err);
- return;
+
+ // Create a promise chain to insert each image in the files array only after the previous one is completed.
+ // p = Promise.resolve() is used to start the chain with a resolved promise.
+ let p = Promise.resolve();
+ for (const file of files) {
+ // Check if the file is an image
+ if (!file.type.includes('image')) {
+ continue;
+ }
+ p = p.finally(() => se.insertImage(file, currentRange));
}
+ // Add the resolve to the end of the promise chain
+ p.finally(() => {
+ // Remove busy class
+ se.editor.classList.remove('se-image-uploading');
+ se._isImageUploading = false;
+
+ // Check image overflow
+ setTimeout(() => se.overflow(), 500);
+
+ resolve();
+ });
+ });
+ }
+
+ /**
+ * Inserts image into the editor, uploads to the server if imageUrl is provided otherwise uses data:image url
+ * @param {*} file
+ * @returns
+ */
+ insertImage(file, range) {
+ const se = this;
+
+ return new Promise((resolveImageInsert) => {
+ const bodyWidth = se.body.offsetWidth;
+
getImageDataURL(file)
.then((imgDataUrl) => {
getImageDimensions(imgDataUrl)
@@ -3477,53 +3474,44 @@ export default class StrivenEditor {
//upload image if imageUrl is provided
se.getImage(imgDataUrl)
.then((imageUrl) => {
- // Replace the temporary image with the uploaded image
- tempImg.outerHTML = `<img src="${imageUrl}" width="${newWidth}px" alt=""/>`;
+ const img = createImageElement(imageUrl, newWidth);
- se._isImageUploading = false;
+ // Ensre that any selected contents is deleted before inserting
+ se.insertNode(img, range);
- setTimeout(function () {
- se.overflow();
- }, 1000);
+ resolveImageInsert();
})
.catch(() => {
// Create the image tag to insert
- const imgTag = `<img src="${imgDataUrl}" width="${newWidth}px" alt="" data-data-url="true"/>`;
- // Replace the temporary image with the uploaded image
- tempImg.outerHTML = imgTag;
- se._isImageUploading = false;
+ const img = createImageElement(imgDataUrl, newWidth);
+ img.setAttribute('data-data-url', 'true');
+
+ // Ensre that any selected contents is deleted before inserting
+ se.insertNode(img, range);
+
+ resolveImageInsert();
});
} else {
// Create the image tag to insert
- const imgTag = `<img src="${imgDataUrl}" width="${newWidth}px" alt="" data-data-url="true"/>`;
- // Replace the temporary image with the uploaded image
- tempImg.outerHTML = imgTag;
- se._isImageUploading = false;
- setTimeout(function () {
- se.overflow();
- }, 100);
- }
+ // Create the image tag to insert
+ const img = createImageElement(imgDataUrl, newWidth);
+ img.setAttribute('data-data-url', 'true');
+
+ // Ensre that any selected contents is deleted before inserting
+ se.insertNode(img, range);
- resolveImageInsert();
+ resolveImageInsert();
+ }
})
.catch((err) => {
console.error(err);
- // Remove temp image
- tempImg.remove();
- se._isImageUploading = false;
});
})
.catch((err) => {
console.error(err);
- // Remove temp image
- tempImg.remove();
- se._isImageUploading = false;
});
}).catch((err) => {
console.error(err);
- // Remove Temp Image
- tempImg.remove();
- se._isImageUploading = false;
});
}
diff --git a/src/utils.js b/src/utils.js
@@ -98,7 +98,7 @@ export const blowUpElement = (el, bg = '#fff', onExpand) => {
export const getImageDimensions = (url) => {
return new Promise((resolve, reject) => {
const img = new Image();
- img.onload = () => resolve({width: img.width, height: img.height});
+ img.onload = () => resolve({ width: img.width, height: img.height });
img.onerror = () => reject('Error loading image. ');
img.src = url;
});
@@ -126,9 +126,9 @@ export const computeImageDimensions = (imageWidth, imageHeight, bodyWidth) => {
// Calculate the ratio of image width to height
const ratio = imageWidth / imageHeight;
// Calculate the new height
- const newHeight = newWidth / ratio;
+ const newHeight = newWidth / ratio;
- return {width: newWidth, height: newHeight};
+ return { width: newWidth, height: newHeight };
};
/**
@@ -143,3 +143,17 @@ export const getImageDataURL = (file) =>
reader.onload = () => resolve(reader.result);
reader.onerror = (error) => reject(error);
});
+
+/**
+ * Creates an image element
+ * @param {*} src
+ * @param {*} width
+ */
+export const createImageElement = (src, width) => {
+ const img = document.createElement('img');
+ img.src = src;
+ img.width = width;
+ img.alt = '';
+
+ return img;
+};