Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function replaceTextPreservingFullStyle(textNode: TextNode, newText: string) {
- if (textNode.characters.length === 0) return;
- const fontName = textNode.getRangeFontName(0, 1) as FontName;
- const fontSize = textNode.getRangeFontSize(0, 1) as number;
- const styleId = textNode.getRangeTextStyleId(0, 1);
- const fills = textNode.getRangeFills(0, 1);
- await figma.loadFontAsync(fontName);
- // Сохраняем цвет, только если он переопределён вручную
- const customFill = fills !== figma.mixed ? (fills as Paint[]) : null;
- // Заменяем текст
- textNode.characters = newText;
- // Применяем стиль, если был
- if (styleId && styleId !== figma.mixed && styleId !== "") {
- await textNode.setRangeTextStyleIdAsync(0, newText.length, styleId as string);
- } else {
- // Иначе применяем вручную всё
- textNode.setRangeFontName(0, newText.length, fontName);
- textNode.setRangeFontSize(0, newText.length, fontSize);
- }
- // Если цвет был переопределён вручную — применим его обратно
- if (customFill) {
- textNode.setRangeFills(0, newText.length, customFill);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement