]> BookStack Code Mirror - bookstack/blobdiff - resources/js/wysiwyg/utils/dom.ts
Lexical: Added media resize support via drag handles
[bookstack] / resources / js / wysiwyg / utils / dom.ts
index 7426ac5925290dd6d43c1d2863293194e5cd7a76..d5c63a816e45f1fdf468a8cc4775ee0a6134f16a 100644 (file)
@@ -29,4 +29,45 @@ export function formatSizeValue(size: number | string, defaultSuffix: string = '
     }
 
     return size;
+}
+
+export function sizeToPixels(size: string): number {
+    if (/^-?\d+$/.test(size)) {
+        return Number(size);
+    }
+
+    if (/^-?\d+\.\d+$/.test(size)) {
+        return Math.round(Number(size));
+    }
+
+    if (/^-?\d+px\s*$/.test(size)) {
+        return Number(size.trim().replace('px', ''));
+    }
+
+    return 0;
+}
+
+export type StyleMap = Map<string, string>;
+
+/**
+ * Creates a map from an element's styles.
+ * Uses direct attribute value string handling since attempting to iterate
+ * over .style will expand out any shorthand properties (like 'padding') making
+ * rather than being representative of the actual properties set.
+ */
+export function extractStyleMapFromElement(element: HTMLElement): StyleMap {
+    const map: StyleMap = new Map();
+    const styleText= element.getAttribute('style') || '';
+
+    const rules = styleText.split(';');
+    for (const rule of rules) {
+        const [name, value] = rule.split(':');
+        if (!name || !value) {
+            continue;
+        }
+
+        map.set(name.trim().toLowerCase(), value.trim());
+    }
+
+    return map;
 }
\ No newline at end of file