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

utils.js (4484B)


      1 /**
      2  * Constructs an SVG from a viewBox and d property
      3  * @param {Object} Object with viewBox and d property
      4  * @param {String} Fill color for the svg
      5  * @param {Number} Height of the SVG
      6  * @param {Width} Width of the SVG
      7  * @returns {HTMLElement} Returns SVG Element
      8  */
      9 export const createSVG = (svgData, fillColor = '#000', height = 16, width = 16) => {
     10     const { viewBox, d } = svgData;
     11     const xmlns = 'http://www.w3.org/2000/svg';
     12 
     13     const icon = document.createElement('div');
     14     const svg = `<svg width="${width}" height="${height}" viewBox="${viewBox}" xmlns="${xmlns}">`;
     15     const path = `<path fill="${fillColor}" d="${d}"/>`;
     16 
     17     icon.innerHTML = `${svg}${path}</svg>`;
     18 
     19     return icon.querySelector('svg');
     20 };
     21 
     22 /**
     23  * Converts camel case string to a readable sentence
     24  * @param {String} text camel case string to convert
     25  * @returns {String} Readable sentence
     26  */
     27 export const denormalizeCamel = (text) => {
     28     const result = text.replace(/([A-Z])/g, ' $1');
     29     const finalResult = result.charAt(0).toUpperCase() + result.slice(1);
     30 
     31     return finalResult;
     32 };
     33 
     34 /**
     35  * Expand DOM element
     36  * @param {HTMLElement} el Element to expand
     37  * @param {String} bg Background color of expanded content
     38  * @param {Function} onExpand Handler function called when element is expanded
     39  */
     40 export const blowUpElement = (el, bg = '#fff', onExpand) => {
     41     function reset() {
     42         el.dataset.expanded = 'false';
     43         el.style.height = null;
     44         el.style.overflow = null;
     45         el.style.position = null;
     46         el.style.top = null;
     47         el.style.bottom = null;
     48         el.style.left = null;
     49         el.style.right = null;
     50         el.style.backgroundColor = null;
     51         el.style.zIndex = null;
     52         el.oncollapse && el.oncollapse();
     53     }
     54 
     55     el.dataset.expanded = 'true';
     56     el.style.height = 'auto';
     57     el.style.overflow = 'auto';
     58     el.style.position = 'fixed';
     59     el.style.top = '0';
     60     el.style.bottom = '0';
     61     el.style.left = '0';
     62     el.style.right = '0';
     63     el.style.backgroundColor = bg;
     64     el.style.zIndex = '1050';
     65 
     66     onExpand && onExpand(el);
     67     el.collapse = () => reset();
     68 
     69     const escapeEvt = (e) => {
     70         if (e.key) {
     71             switch (e.key) {
     72                 case 'Escape':
     73                     el.dataset.expanded === 'true' && reset();
     74                     break;
     75                 default:
     76                     break;
     77             }
     78         } else {
     79             switch (e.keyCode) {
     80                 case 13:
     81                     el.dataset.expanded === 'true' && reset();
     82                     break;
     83                 default:
     84                     break;
     85             }
     86         }
     87     };
     88 
     89     window.addEventListener('keydown', escapeEvt);
     90     el.addEventListener('keydown', escapeEvt);
     91 };
     92 
     93 /**
     94  * Gets the width of an image
     95  * @param {string} url
     96  * @returns
     97  */
     98 export const getImageDimensions = (url) => {
     99     return new Promise((resolve, reject) => {
    100         const img = new Image();
    101         img.onload = () => resolve({ width: img.width, height: img.height });
    102         img.onerror = () => reject('Error loading image. ');
    103         img.src = url;
    104     });
    105 };
    106 
    107 /**
    108  * Computes the width of an image based on the body width
    109  * @param {Int} imageWidth
    110  * @param {Int} imageHeight
    111  * @param {Int} bodyWidth
    112  * @returns {Int} computed width
    113  */
    114 export const computeImageDimensions = (imageWidth, imageHeight, bodyWidth) => {
    115     let newWidth = imageWidth;
    116     // If the image is wider than the body, set the width to 90% of the body width
    117     if (imageWidth > bodyWidth) {
    118         // Set the width to 90%
    119         newWidth = bodyWidth * 0.9;
    120     }
    121 
    122     if (newWidth > 1200) {
    123         newWidth = 1200;
    124     }
    125 
    126     // Calculate the ratio of image width to height
    127     const ratio = imageWidth / imageHeight;
    128     // Calculate the new height
    129     const newHeight = newWidth / ratio;
    130 
    131     return { width: newWidth, height: newHeight };
    132 };
    133 
    134 /**
    135  * Gets the data URL of an image
    136  * @param {File} file
    137  * @returns
    138  */
    139 export const getImageDataURL = (file) =>
    140     new Promise((resolve, reject) => {
    141         const reader = new FileReader();
    142         reader.readAsDataURL(file);
    143         reader.onload = () => resolve(reader.result);
    144         reader.onerror = (error) => reject(error);
    145     });
    146 
    147 /**
    148  * Creates an image element
    149  * @param {*} src
    150  * @param {*} width
    151  */
    152 export const createImageElement = (src, width) => {
    153     const img = document.createElement('img');
    154     img.src = src;
    155     img.width = width;
    156     img.alt = '';
    157 
    158     return img;
    159 };