diff --git a/frontend/text-editor/src/editor/TextEditor.js b/frontend/text-editor/src/editor/TextEditor.js index 657811f04..357496312 100644 --- a/frontend/text-editor/src/editor/TextEditor.js +++ b/frontend/text-editor/src/editor/TextEditor.js @@ -223,7 +223,9 @@ export class TextEditor extends EventTarget { * @param {FocusEvent} e */ #onFocus = (e) => { - this.#selectionController.restoreSelection(); + if (!this.#selectionController.restoreSelection()) { + this.selectAll(); + } if (this.#selectionImposterElement) { this.#selectionImposterElement.replaceChildren(); } diff --git a/frontend/text-editor/src/editor/content/dom/Content.js b/frontend/text-editor/src/editor/content/dom/Content.js index 0a2ba7ff0..2c28f269f 100644 --- a/frontend/text-editor/src/editor/content/dom/Content.js +++ b/frontend/text-editor/src/editor/content/dom/Content.js @@ -49,9 +49,10 @@ export function mapContentFragmentFromDocument(document, root, styleDefaults) { } } - currentParagraph.appendChild( - createInline(new Text(currentNode.nodeValue), currentStyle) - ); + const inline = createInline(new Text(currentNode.nodeValue), currentStyle); + const fontSize = inline.style.getPropertyValue("font-size"); + if (!fontSize) console.warn("font-size", fontSize); + currentParagraph.appendChild(inline); currentNode = nodeIterator.nextNode(); } diff --git a/frontend/text-editor/src/editor/content/dom/Style.js b/frontend/text-editor/src/editor/content/dom/Style.js index 6b563584a..6405273fa 100644 --- a/frontend/text-editor/src/editor/content/dom/Style.js +++ b/frontend/text-editor/src/editor/content/dom/Style.js @@ -173,13 +173,31 @@ export function setStyle(element, styleName, styleValue, styleUnit) { return element; } +/** + * Returns the value of the font size + * + * @param {number} styleValueAsNumber + * @param {string} styleValue + * @returns {string} + */ +function getStyleFontSize(styleValueAsNumber, styleValue) { + if (styleValue.endsWith("pt")) { + return (styleValueAsNumber * 1.3333).toFixed(); + } else if (styleValue.endsWith("em")) { + return (styleValueAsNumber * baseSize).toFixed(); + } else if (styleValue.endsWith("%")) { + return ((styleValueAsNumber / 100) * baseSize).toFixed(); + } + return styleValueAsNumber.toFixed(); +} + /** * Returns the value of a style from a declaration. * * @param {CSSStyleDeclaration} style * @param {string} styleName * @param {string|undefined} [styleUnit] - * @returns {*} + * @returns {string} */ export function getStyleFromDeclaration(style, styleName, styleUnit) { if (styleName.startsWith("--")) { @@ -189,7 +207,14 @@ export function getStyleFromDeclaration(style, styleName, styleUnit) { if (styleValue.endsWith(styleUnit)) { return styleValue.slice(0, -styleUnit.length); } - return styleValue; + const styleValueAsNumber = parseFloat(styleValue); + if (styleName === "font-size") { + return getStyleFontSize(styleValueAsNumber, styleValue); + } + if (Number.isNaN(styleValueAsNumber)) { + return styleValue; + } + return styleValueAsNumber.toFixed(); } /**