mirror of
https://github.com/penpot/penpot.git
synced 2025-01-06 14:50:20 -05:00
🐛 Fix text editor issues
This commit is contained in:
parent
59e5656bd7
commit
59ca09c24e
3 changed files with 34 additions and 6 deletions
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue