@yurikilian/lex4 1.10.0 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ast/index.d.ts +1 -1
- package/dist/ast/index.d.ts.map +1 -1
- package/dist/ast/inline-mapper.d.ts +7 -1
- package/dist/ast/inline-mapper.d.ts.map +1 -1
- package/dist/ast/types.d.ts +9 -1
- package/dist/ast/types.d.ts.map +1 -1
- package/dist/context/toolbar-style-snapshot.d.ts.map +1 -1
- package/dist/context/toolbar-style-store.d.ts +2 -0
- package/dist/context/toolbar-style-store.d.ts.map +1 -1
- package/dist/extensions/variables-extension.d.ts.map +1 -1
- package/dist/i18n/defaults.d.ts.map +1 -1
- package/dist/i18n/pt-BR.d.ts.map +1 -1
- package/dist/i18n/types.d.ts +3 -0
- package/dist/i18n/types.d.ts.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/lex4-editor.cjs +284 -16
- package/dist/lex4-editor.cjs.map +1 -1
- package/dist/lex4-editor.js +285 -17
- package/dist/lex4-editor.js.map +1 -1
- package/dist/lexical/utils/import-document-content.d.ts.map +1 -1
- package/dist/style.css +42 -1
- package/dist/variables/index.d.ts +3 -0
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/optional-segment-commands.d.ts +24 -0
- package/dist/variables/optional-segment-commands.d.ts.map +1 -0
- package/dist/variables/optional-segment-node.d.ts +32 -0
- package/dist/variables/optional-segment-node.d.ts.map +1 -0
- package/dist/variables/optional-segment-plugin.d.ts +6 -0
- package/dist/variables/optional-segment-plugin.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/lex4-editor.js
CHANGED
|
@@ -3,7 +3,7 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
4
|
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
|
|
5
5
|
import React, { createContext, useContext, useMemo, useRef, useReducer, useState, useCallback, useEffect, forwardRef, createElement, useImperativeHandle } from "react";
|
|
6
|
-
import { $getSelection, $isRangeSelection, $isTextNode, $getRoot, $createRangeSelectionFromDom, FORMAT_ELEMENT_COMMAND, FORMAT_TEXT_COMMAND, $applyNodeReplacement, OUTDENT_CONTENT_COMMAND, INDENT_CONTENT_COMMAND, DecoratorNode, $createNodeSelection, $isNodeSelection, $setSelection, KEY_BACKSPACE_COMMAND, COMMAND_PRIORITY_LOW, KEY_DELETE_COMMAND, KEY_DOWN_COMMAND, $getNodeByKey, $isElementNode, $createParagraphNode, $selectAll, SELECTION_CHANGE_COMMAND, KEY_TAB_COMMAND, $isParagraphNode, FOCUS_COMMAND, $splitNode, $getNearestNodeFromDOMNode, CONTROLLED_TEXT_INSERTION_COMMAND, PASTE_COMMAND, KEY_ENTER_COMMAND, COMMAND_PRIORITY_HIGH, COMMAND_PRIORITY_CRITICAL, $insertNodes, $createLineBreakNode, $createTextNode,
|
|
6
|
+
import { $getSelection, $isRangeSelection, $isTextNode, $getRoot, $createRangeSelectionFromDom, FORMAT_ELEMENT_COMMAND, FORMAT_TEXT_COMMAND, $applyNodeReplacement, OUTDENT_CONTENT_COMMAND, INDENT_CONTENT_COMMAND, DecoratorNode, $createNodeSelection, $isNodeSelection, $setSelection, KEY_BACKSPACE_COMMAND, COMMAND_PRIORITY_LOW, KEY_DELETE_COMMAND, KEY_DOWN_COMMAND, $getNodeByKey, $isElementNode, $createParagraphNode, ElementNode, createCommand, $selectAll, SELECTION_CHANGE_COMMAND, KEY_TAB_COMMAND, $isParagraphNode, FOCUS_COMMAND, $splitNode, $getNearestNodeFromDOMNode, CONTROLLED_TEXT_INSERTION_COMMAND, PASTE_COMMAND, KEY_ENTER_COMMAND, COMMAND_PRIORITY_HIGH, COMMAND_PRIORITY_CRITICAL, $insertNodes, $createLineBreakNode, $createTextNode, COMMAND_PRIORITY_EDITOR } from "lexical";
|
|
7
7
|
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
8
8
|
import { ListNode, INSERT_ORDERED_LIST_COMMAND, INSERT_UNORDERED_LIST_COMMAND, REMOVE_LIST_COMMAND, $createListNode, $isListNode, ListItemNode, $createListItemNode } from "@lexical/list";
|
|
9
9
|
import { $findMatchingParent } from "@lexical/utils";
|
|
@@ -518,7 +518,8 @@ const DEFAULT_TRANSLATIONS = {
|
|
|
518
518
|
fontChanged: "Font changed to {{value}}",
|
|
519
519
|
fontSizeChanged: "Font size changed to {{value}}pt",
|
|
520
520
|
blockTypeChanged: "Block type changed to {{value}}",
|
|
521
|
-
insertedDocumentContent: "Inserted document content"
|
|
521
|
+
insertedDocumentContent: "Inserted document content",
|
|
522
|
+
optionalSegmentToggled: "Optional section toggled"
|
|
522
523
|
}
|
|
523
524
|
},
|
|
524
525
|
variables: {
|
|
@@ -539,7 +540,9 @@ const DEFAULT_TRANSLATIONS = {
|
|
|
539
540
|
createVariableSave: "Add variable",
|
|
540
541
|
createVariableCancel: "Cancel",
|
|
541
542
|
createVariableMissingFields: "Label and key are required.",
|
|
542
|
-
createVariableDuplicateKey: "Variable {{key}} already exists."
|
|
543
|
+
createVariableDuplicateKey: "Variable {{key}} already exists.",
|
|
544
|
+
optionalSegmentToggle: "Optional section",
|
|
545
|
+
optionalSegmentTooltip: "Optional section — appears only when all fields have a value"
|
|
543
546
|
},
|
|
544
547
|
header: {
|
|
545
548
|
placeholder: "Header"
|
|
@@ -662,7 +665,8 @@ const PT_BR_TRANSLATIONS = {
|
|
|
662
665
|
fontChanged: "Fonte alterada para {{value}}",
|
|
663
666
|
fontSizeChanged: "Tamanho da fonte alterado para {{value}}pt",
|
|
664
667
|
blockTypeChanged: "Tipo de bloco alterado para {{value}}",
|
|
665
|
-
insertedDocumentContent: "Conteúdo do documento inserido"
|
|
668
|
+
insertedDocumentContent: "Conteúdo do documento inserido",
|
|
669
|
+
optionalSegmentToggled: "Seção opcional alternada"
|
|
666
670
|
}
|
|
667
671
|
},
|
|
668
672
|
variables: {
|
|
@@ -683,7 +687,9 @@ const PT_BR_TRANSLATIONS = {
|
|
|
683
687
|
createVariableSave: "Adicionar variável",
|
|
684
688
|
createVariableCancel: "Cancelar",
|
|
685
689
|
createVariableMissingFields: "Rótulo e chave são obrigatórios.",
|
|
686
|
-
createVariableDuplicateKey: "A variável {{key}} já existe."
|
|
690
|
+
createVariableDuplicateKey: "A variável {{key}} já existe.",
|
|
691
|
+
optionalSegmentToggle: "Seção opcional",
|
|
692
|
+
optionalSegmentTooltip: "Seção opcional — aparece apenas quando todos os campos têm valor"
|
|
687
693
|
},
|
|
688
694
|
header: {
|
|
689
695
|
placeholder: "Cabeçalho"
|
|
@@ -861,7 +867,9 @@ const DEFAULT_TOOLBAR_STYLE_SNAPSHOT = {
|
|
|
861
867
|
isItalic: false,
|
|
862
868
|
isUnderline: false,
|
|
863
869
|
isStrikethrough: false,
|
|
864
|
-
hasSelectedVariable: false
|
|
870
|
+
hasSelectedVariable: false,
|
|
871
|
+
hasTextSelection: false,
|
|
872
|
+
insideOptionalSegment: false
|
|
865
873
|
};
|
|
866
874
|
function createToolbarStyleStore(initialSnapshot = DEFAULT_TOOLBAR_STYLE_SNAPSHOT) {
|
|
867
875
|
return createStore((set) => ({
|
|
@@ -1577,33 +1585,33 @@ const createLucideIcon = (iconName, iconNode) => {
|
|
|
1577
1585
|
* This source code is licensed under the ISC license.
|
|
1578
1586
|
* See the LICENSE file in the root directory of this source tree.
|
|
1579
1587
|
*/
|
|
1580
|
-
const __iconNode$
|
|
1588
|
+
const __iconNode$s = [
|
|
1581
1589
|
["path", { d: "m15 16 2.536-7.328a1.02 1.02 1 0 1 1.928 0L22 16", key: "xik6mr" }],
|
|
1582
1590
|
["path", { d: "M15.697 14h5.606", key: "1stdlc" }],
|
|
1583
1591
|
["path", { d: "m2 16 4.039-9.69a.5.5 0 0 1 .923 0L11 16", key: "d5nyq2" }],
|
|
1584
1592
|
["path", { d: "M3.304 13h6.392", key: "1q3zxz" }]
|
|
1585
1593
|
];
|
|
1586
|
-
const ALargeSmall = createLucideIcon("a-large-small", __iconNode$
|
|
1594
|
+
const ALargeSmall = createLucideIcon("a-large-small", __iconNode$s);
|
|
1587
1595
|
/**
|
|
1588
1596
|
* @license lucide-react v1.8.0 - ISC
|
|
1589
1597
|
*
|
|
1590
1598
|
* This source code is licensed under the ISC license.
|
|
1591
1599
|
* See the LICENSE file in the root directory of this source tree.
|
|
1592
1600
|
*/
|
|
1593
|
-
const __iconNode$
|
|
1601
|
+
const __iconNode$r = [
|
|
1594
1602
|
[
|
|
1595
1603
|
"path",
|
|
1596
1604
|
{ d: "M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8", key: "mg9rjx" }
|
|
1597
1605
|
]
|
|
1598
1606
|
];
|
|
1599
|
-
const Bold = createLucideIcon("bold", __iconNode$
|
|
1607
|
+
const Bold = createLucideIcon("bold", __iconNode$r);
|
|
1600
1608
|
/**
|
|
1601
1609
|
* @license lucide-react v1.8.0 - ISC
|
|
1602
1610
|
*
|
|
1603
1611
|
* This source code is licensed under the ISC license.
|
|
1604
1612
|
* See the LICENSE file in the root directory of this source tree.
|
|
1605
1613
|
*/
|
|
1606
|
-
const __iconNode$
|
|
1614
|
+
const __iconNode$q = [
|
|
1607
1615
|
[
|
|
1608
1616
|
"path",
|
|
1609
1617
|
{ d: "M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1", key: "ezmyqa" }
|
|
@@ -1616,7 +1624,18 @@ const __iconNode$p = [
|
|
|
1616
1624
|
}
|
|
1617
1625
|
]
|
|
1618
1626
|
];
|
|
1619
|
-
const Braces = createLucideIcon("braces", __iconNode$
|
|
1627
|
+
const Braces = createLucideIcon("braces", __iconNode$q);
|
|
1628
|
+
/**
|
|
1629
|
+
* @license lucide-react v1.8.0 - ISC
|
|
1630
|
+
*
|
|
1631
|
+
* This source code is licensed under the ISC license.
|
|
1632
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
1633
|
+
*/
|
|
1634
|
+
const __iconNode$p = [
|
|
1635
|
+
["path", { d: "M16 3h3a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1h-3", key: "1kt8lf" }],
|
|
1636
|
+
["path", { d: "M8 21H5a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1h3", key: "gduv9" }]
|
|
1637
|
+
];
|
|
1638
|
+
const Brackets = createLucideIcon("brackets", __iconNode$p);
|
|
1620
1639
|
/**
|
|
1621
1640
|
* @license lucide-react v1.8.0 - ISC
|
|
1622
1641
|
*
|
|
@@ -2746,7 +2765,7 @@ function VariableChip({
|
|
|
2746
2765
|
className,
|
|
2747
2766
|
"data-testid": `variable-chip-${variableKey}`,
|
|
2748
2767
|
"data-variable-group": group,
|
|
2749
|
-
title:
|
|
2768
|
+
title: label,
|
|
2750
2769
|
style,
|
|
2751
2770
|
onMouseDown: handleMouseDown,
|
|
2752
2771
|
onMouseUp: handleMouseUp,
|
|
@@ -3407,6 +3426,154 @@ const CanvasControls = () => {
|
|
|
3407
3426
|
)
|
|
3408
3427
|
] });
|
|
3409
3428
|
};
|
|
3429
|
+
class OptionalSegmentNode extends ElementNode {
|
|
3430
|
+
static getType() {
|
|
3431
|
+
return "optional-segment";
|
|
3432
|
+
}
|
|
3433
|
+
static clone(node) {
|
|
3434
|
+
return new OptionalSegmentNode(node.__key);
|
|
3435
|
+
}
|
|
3436
|
+
constructor(key) {
|
|
3437
|
+
super(key);
|
|
3438
|
+
}
|
|
3439
|
+
// -- Serialization --
|
|
3440
|
+
static importJSON(serializedNode) {
|
|
3441
|
+
const node = $createOptionalSegmentNode();
|
|
3442
|
+
node.setFormat(serializedNode.format);
|
|
3443
|
+
node.setIndent(serializedNode.indent);
|
|
3444
|
+
node.setDirection(serializedNode.direction);
|
|
3445
|
+
return node;
|
|
3446
|
+
}
|
|
3447
|
+
exportJSON() {
|
|
3448
|
+
return {
|
|
3449
|
+
...super.exportJSON(),
|
|
3450
|
+
type: "optional-segment",
|
|
3451
|
+
version: 1
|
|
3452
|
+
};
|
|
3453
|
+
}
|
|
3454
|
+
// -- DOM --
|
|
3455
|
+
createDOM() {
|
|
3456
|
+
const span = document.createElement("span");
|
|
3457
|
+
span.className = "lex4-optional-segment";
|
|
3458
|
+
span.setAttribute("data-lex4-optional-segment", "true");
|
|
3459
|
+
span.setAttribute("data-testid", "optional-segment");
|
|
3460
|
+
return span;
|
|
3461
|
+
}
|
|
3462
|
+
updateDOM() {
|
|
3463
|
+
return false;
|
|
3464
|
+
}
|
|
3465
|
+
exportDOM() {
|
|
3466
|
+
const span = document.createElement("span");
|
|
3467
|
+
span.setAttribute("data-lex4-optional-segment", "true");
|
|
3468
|
+
return { element: span };
|
|
3469
|
+
}
|
|
3470
|
+
static importDOM() {
|
|
3471
|
+
return null;
|
|
3472
|
+
}
|
|
3473
|
+
// -- Behavior --
|
|
3474
|
+
isInline() {
|
|
3475
|
+
return true;
|
|
3476
|
+
}
|
|
3477
|
+
canBeEmpty() {
|
|
3478
|
+
return false;
|
|
3479
|
+
}
|
|
3480
|
+
canInsertTextBefore() {
|
|
3481
|
+
return false;
|
|
3482
|
+
}
|
|
3483
|
+
canInsertTextAfter() {
|
|
3484
|
+
return false;
|
|
3485
|
+
}
|
|
3486
|
+
insertNewAfter(_, restoreSelection = true) {
|
|
3487
|
+
const segment = $createOptionalSegmentNode();
|
|
3488
|
+
this.insertAfter(segment, restoreSelection);
|
|
3489
|
+
return segment;
|
|
3490
|
+
}
|
|
3491
|
+
extractWithChild(_child, selection, _destination) {
|
|
3492
|
+
if (!$isRangeSelection(selection)) {
|
|
3493
|
+
return false;
|
|
3494
|
+
}
|
|
3495
|
+
const anchorNode = selection.anchor.getNode();
|
|
3496
|
+
const focusNode = selection.focus.getNode();
|
|
3497
|
+
return this.isParentOf(anchorNode) && this.isParentOf(focusNode) && selection.getTextContent().length > 0;
|
|
3498
|
+
}
|
|
3499
|
+
}
|
|
3500
|
+
function $createOptionalSegmentNode() {
|
|
3501
|
+
return $applyNodeReplacement(new OptionalSegmentNode());
|
|
3502
|
+
}
|
|
3503
|
+
function $isOptionalSegmentNode(node) {
|
|
3504
|
+
return node instanceof OptionalSegmentNode;
|
|
3505
|
+
}
|
|
3506
|
+
const TOGGLE_OPTIONAL_SEGMENT_COMMAND = createCommand(
|
|
3507
|
+
"TOGGLE_OPTIONAL_SEGMENT"
|
|
3508
|
+
);
|
|
3509
|
+
function $getAncestorOptionalSegment(node) {
|
|
3510
|
+
let current = node;
|
|
3511
|
+
while (current !== null) {
|
|
3512
|
+
if ($isOptionalSegmentNode(current)) {
|
|
3513
|
+
return current;
|
|
3514
|
+
}
|
|
3515
|
+
current = typeof current.getParent === "function" ? current.getParent() : null;
|
|
3516
|
+
}
|
|
3517
|
+
return null;
|
|
3518
|
+
}
|
|
3519
|
+
function $unwrapOptionalSegment(segment) {
|
|
3520
|
+
for (const child of segment.getChildren()) {
|
|
3521
|
+
segment.insertBefore(child);
|
|
3522
|
+
}
|
|
3523
|
+
segment.remove();
|
|
3524
|
+
}
|
|
3525
|
+
function containsOptionalSegment(node) {
|
|
3526
|
+
if ($isOptionalSegmentNode(node)) {
|
|
3527
|
+
return true;
|
|
3528
|
+
}
|
|
3529
|
+
if ($isElementNode(node)) {
|
|
3530
|
+
return node.getChildren().some(containsOptionalSegment);
|
|
3531
|
+
}
|
|
3532
|
+
return false;
|
|
3533
|
+
}
|
|
3534
|
+
function $toggleOptionalSegment() {
|
|
3535
|
+
const selection = $getSelection();
|
|
3536
|
+
if (!$isRangeSelection(selection)) {
|
|
3537
|
+
return false;
|
|
3538
|
+
}
|
|
3539
|
+
const anchorSegment = $getAncestorOptionalSegment(selection.anchor.getNode());
|
|
3540
|
+
const focusSegment = $getAncestorOptionalSegment(selection.focus.getNode());
|
|
3541
|
+
if (anchorSegment !== null || focusSegment !== null) {
|
|
3542
|
+
if (anchorSegment !== null && focusSegment !== null && anchorSegment !== focusSegment) {
|
|
3543
|
+
return false;
|
|
3544
|
+
}
|
|
3545
|
+
$unwrapOptionalSegment(anchorSegment ?? focusSegment);
|
|
3546
|
+
return true;
|
|
3547
|
+
}
|
|
3548
|
+
if (selection.isCollapsed()) {
|
|
3549
|
+
return false;
|
|
3550
|
+
}
|
|
3551
|
+
const anchorTop = selection.anchor.getNode().getTopLevelElement();
|
|
3552
|
+
const focusTop = selection.focus.getNode().getTopLevelElement();
|
|
3553
|
+
if (anchorTop === null || anchorTop !== focusTop) {
|
|
3554
|
+
return false;
|
|
3555
|
+
}
|
|
3556
|
+
const extracted = selection.extract();
|
|
3557
|
+
const inlineNodes = extracted.filter((node) => {
|
|
3558
|
+
if ($isElementNode(node) && !node.isInline()) {
|
|
3559
|
+
return false;
|
|
3560
|
+
}
|
|
3561
|
+
return !node.getParents().some((parent) => extracted.includes(parent));
|
|
3562
|
+
});
|
|
3563
|
+
if (inlineNodes.length === 0) {
|
|
3564
|
+
return false;
|
|
3565
|
+
}
|
|
3566
|
+
if (inlineNodes.some(containsOptionalSegment)) {
|
|
3567
|
+
return false;
|
|
3568
|
+
}
|
|
3569
|
+
const segment = $createOptionalSegmentNode();
|
|
3570
|
+
inlineNodes[0].insertBefore(segment);
|
|
3571
|
+
for (const node of inlineNodes) {
|
|
3572
|
+
segment.append(node);
|
|
3573
|
+
}
|
|
3574
|
+
segment.selectEnd();
|
|
3575
|
+
return true;
|
|
3576
|
+
}
|
|
3410
3577
|
const FORMAT_MASKS = {
|
|
3411
3578
|
bold: 1,
|
|
3412
3579
|
italic: 2,
|
|
@@ -3500,6 +3667,7 @@ function hasInlineFormat(node, format) {
|
|
|
3500
3667
|
function readToolbarStyleSnapshot(editor, editorState = editor.getEditorState()) {
|
|
3501
3668
|
let snapshot = DEFAULT_TOOLBAR_STYLE_SNAPSHOT;
|
|
3502
3669
|
editorState.read(() => {
|
|
3670
|
+
var _a;
|
|
3503
3671
|
const currentSelection = $getSelection();
|
|
3504
3672
|
const selection = $isNodeSelection(currentSelection) || $isRangeSelection(currentSelection) ? currentSelection : $createRangeSelectionFromDom(window.getSelection(), editor) ?? currentSelection;
|
|
3505
3673
|
if ($isNodeSelection(selection)) {
|
|
@@ -3519,7 +3687,9 @@ function readToolbarStyleSnapshot(editor, editorState = editor.getEditorState())
|
|
|
3519
3687
|
isItalic: variableNodes.every((node) => (node.getFormat() & FORMAT_MASKS.italic) !== 0),
|
|
3520
3688
|
isUnderline: variableNodes.every((node) => (node.getFormat() & FORMAT_MASKS.underline) !== 0),
|
|
3521
3689
|
isStrikethrough: variableNodes.every((node) => (node.getFormat() & FORMAT_MASKS.strikethrough) !== 0),
|
|
3522
|
-
hasSelectedVariable: true
|
|
3690
|
+
hasSelectedVariable: true,
|
|
3691
|
+
hasTextSelection: false,
|
|
3692
|
+
insideOptionalSegment: $getAncestorOptionalSegment(firstVariableNode) !== null
|
|
3523
3693
|
};
|
|
3524
3694
|
return;
|
|
3525
3695
|
}
|
|
@@ -3540,7 +3710,9 @@ function readToolbarStyleSnapshot(editor, editorState = editor.getEditorState())
|
|
|
3540
3710
|
isItalic: selection.hasFormat("italic") || isCollapsed && hasInlineFormat(inlineStyleTarget, "italic"),
|
|
3541
3711
|
isUnderline: selection.hasFormat("underline") || isCollapsed && hasInlineFormat(inlineStyleTarget, "underline"),
|
|
3542
3712
|
isStrikethrough: selection.hasFormat("strikethrough") || isCollapsed && hasInlineFormat(inlineStyleTarget, "strikethrough"),
|
|
3543
|
-
hasSelectedVariable: false
|
|
3713
|
+
hasSelectedVariable: false,
|
|
3714
|
+
hasTextSelection: !isCollapsed,
|
|
3715
|
+
insideOptionalSegment: $getAncestorOptionalSegment(anchorNode) !== null || $getAncestorOptionalSegment(((_a = selection.focus) == null ? void 0 : _a.getNode()) ?? null) !== null
|
|
3544
3716
|
};
|
|
3545
3717
|
});
|
|
3546
3718
|
return snapshot;
|
|
@@ -5798,6 +5970,8 @@ function mapInlineNode(node) {
|
|
|
5798
5970
|
return mapVariableNode(node);
|
|
5799
5971
|
case "linebreak":
|
|
5800
5972
|
return mapLineBreak();
|
|
5973
|
+
case "optional-segment":
|
|
5974
|
+
return mapOptionalSegment(node);
|
|
5801
5975
|
default:
|
|
5802
5976
|
return { type: "text", text: "" };
|
|
5803
5977
|
}
|
|
@@ -5821,6 +5995,12 @@ function mapVariableNode(node) {
|
|
|
5821
5995
|
function mapLineBreak() {
|
|
5822
5996
|
return { type: "linebreak" };
|
|
5823
5997
|
}
|
|
5998
|
+
function mapOptionalSegment(node) {
|
|
5999
|
+
return {
|
|
6000
|
+
type: "optional-segment",
|
|
6001
|
+
children: mapInlineNodes(node.children ?? [])
|
|
6002
|
+
};
|
|
6003
|
+
}
|
|
5824
6004
|
function mapInlineNodes(nodes) {
|
|
5825
6005
|
return nodes.map(mapInlineNode);
|
|
5826
6006
|
}
|
|
@@ -6076,6 +6256,11 @@ function buildLexicalNode(serializedNode) {
|
|
|
6076
6256
|
typeof serializedNode.format === "number" ? serializedNode.format : 0,
|
|
6077
6257
|
typeof serializedNode.style === "string" ? serializedNode.style : ""
|
|
6078
6258
|
);
|
|
6259
|
+
case "optional-segment": {
|
|
6260
|
+
const node = $createOptionalSegmentNode();
|
|
6261
|
+
appendChildren(node, serializedNode.children);
|
|
6262
|
+
return node;
|
|
6263
|
+
}
|
|
6079
6264
|
default:
|
|
6080
6265
|
return null;
|
|
6081
6266
|
}
|
|
@@ -6557,6 +6742,42 @@ const VariablePlugin = () => {
|
|
|
6557
6742
|
}, [editor]);
|
|
6558
6743
|
return null;
|
|
6559
6744
|
};
|
|
6745
|
+
const OptionalSegmentPlugin = () => {
|
|
6746
|
+
const [editor] = useLexicalComposerContext();
|
|
6747
|
+
const t = useTranslations();
|
|
6748
|
+
useEffect(() => {
|
|
6749
|
+
return editor.registerCommand(
|
|
6750
|
+
TOGGLE_OPTIONAL_SEGMENT_COMMAND,
|
|
6751
|
+
() => {
|
|
6752
|
+
let handled = false;
|
|
6753
|
+
editor.update(() => {
|
|
6754
|
+
handled = $toggleOptionalSegment();
|
|
6755
|
+
});
|
|
6756
|
+
return handled;
|
|
6757
|
+
},
|
|
6758
|
+
COMMAND_PRIORITY_EDITOR
|
|
6759
|
+
);
|
|
6760
|
+
}, [editor]);
|
|
6761
|
+
useEffect(() => {
|
|
6762
|
+
return editor.registerMutationListener(
|
|
6763
|
+
OptionalSegmentNode,
|
|
6764
|
+
(mutations) => {
|
|
6765
|
+
editor.getEditorState().read(() => {
|
|
6766
|
+
for (const [key, mutation] of mutations) {
|
|
6767
|
+
if (mutation === "created" || mutation === "updated") {
|
|
6768
|
+
const element = editor.getElementByKey(key);
|
|
6769
|
+
if (element) {
|
|
6770
|
+
element.title = t.variables.optionalSegmentTooltip;
|
|
6771
|
+
}
|
|
6772
|
+
}
|
|
6773
|
+
}
|
|
6774
|
+
});
|
|
6775
|
+
},
|
|
6776
|
+
{ skipInitialization: false }
|
|
6777
|
+
);
|
|
6778
|
+
}, [editor, t.variables.optionalSegmentTooltip]);
|
|
6779
|
+
return null;
|
|
6780
|
+
};
|
|
6560
6781
|
const VariablePanel = ({ open, onClose }) => {
|
|
6561
6782
|
const { definitions, refreshDefinitions } = useVariables();
|
|
6562
6783
|
const { activeEditor, runHistoryAction } = useDocument();
|
|
@@ -6850,6 +7071,48 @@ const VariableToolbarToggle = () => {
|
|
|
6850
7071
|
}
|
|
6851
7072
|
);
|
|
6852
7073
|
};
|
|
7074
|
+
const OptionalSegmentToolbarButton = () => {
|
|
7075
|
+
const { activeEditor, runHistoryAction } = useDocument();
|
|
7076
|
+
const toolbarConfig = useToolbarConfig();
|
|
7077
|
+
const t = useTranslations();
|
|
7078
|
+
const hasTextSelection = useToolbarStyleStore((state) => state.hasTextSelection);
|
|
7079
|
+
const insideOptionalSegment = useToolbarStyleStore((state) => state.insideOptionalSegment);
|
|
7080
|
+
if (!toolbarConfig.variables.visible) {
|
|
7081
|
+
return null;
|
|
7082
|
+
}
|
|
7083
|
+
if (!hasTextSelection && !insideOptionalSegment) {
|
|
7084
|
+
return null;
|
|
7085
|
+
}
|
|
7086
|
+
const handleClick = () => {
|
|
7087
|
+
if (!activeEditor) {
|
|
7088
|
+
return;
|
|
7089
|
+
}
|
|
7090
|
+
runHistoryAction(
|
|
7091
|
+
{
|
|
7092
|
+
label: t.history.actions.optionalSegmentToggled,
|
|
7093
|
+
source: "toolbar",
|
|
7094
|
+
region: "document"
|
|
7095
|
+
},
|
|
7096
|
+
() => {
|
|
7097
|
+
activeEditor.dispatchCommand(TOGGLE_OPTIONAL_SEGMENT_COMMAND, void 0);
|
|
7098
|
+
}
|
|
7099
|
+
);
|
|
7100
|
+
};
|
|
7101
|
+
return /* @__PURE__ */ jsx(
|
|
7102
|
+
"button",
|
|
7103
|
+
{
|
|
7104
|
+
type: "button",
|
|
7105
|
+
title: t.variables.optionalSegmentToggle,
|
|
7106
|
+
"aria-label": t.variables.optionalSegmentToggle,
|
|
7107
|
+
"aria-pressed": insideOptionalSegment,
|
|
7108
|
+
onMouseDown: (e) => e.preventDefault(),
|
|
7109
|
+
onClick: handleClick,
|
|
7110
|
+
className: `lex4-toolbar-btn${insideOptionalSegment ? " active" : ""}`,
|
|
7111
|
+
"data-testid": "btn-optional-segment",
|
|
7112
|
+
children: /* @__PURE__ */ jsx(Brackets, { size: 15 })
|
|
7113
|
+
}
|
|
7114
|
+
);
|
|
7115
|
+
};
|
|
6853
7116
|
const VariablePanelWithState = () => {
|
|
6854
7117
|
const { panelOpen, setPanelOpen } = useVariablePanelState();
|
|
6855
7118
|
return /* @__PURE__ */ jsx(VariablePanel, { open: panelOpen, onClose: () => setPanelOpen(false) });
|
|
@@ -6877,8 +7140,9 @@ function variablesExtension(definitions = []) {
|
|
|
6877
7140
|
};
|
|
6878
7141
|
return {
|
|
6879
7142
|
name: "variables",
|
|
6880
|
-
nodes: [VariableNode],
|
|
6881
|
-
bodyPlugins: [VariablePlugin],
|
|
7143
|
+
nodes: [VariableNode, OptionalSegmentNode],
|
|
7144
|
+
bodyPlugins: [VariablePlugin, OptionalSegmentPlugin],
|
|
7145
|
+
toolbarItems: [OptionalSegmentToolbarButton],
|
|
6882
7146
|
toolbarEndItems: [VariableToolbarToggle],
|
|
6883
7147
|
sidePanel: VariablePanelWithState,
|
|
6884
7148
|
provider: ProviderWrapper,
|
|
@@ -6905,7 +7169,9 @@ function variablesExtension(definitions = []) {
|
|
|
6905
7169
|
};
|
|
6906
7170
|
}
|
|
6907
7171
|
export {
|
|
7172
|
+
$createOptionalSegmentNode,
|
|
6908
7173
|
$createVariableNode,
|
|
7174
|
+
$isOptionalSegmentNode,
|
|
6909
7175
|
$isVariableNode,
|
|
6910
7176
|
A4_HEIGHT_MM,
|
|
6911
7177
|
A4_HEIGHT_PX,
|
|
@@ -6917,7 +7183,9 @@ export {
|
|
|
6917
7183
|
Lex4Editor,
|
|
6918
7184
|
MAX_FOOTER_HEIGHT_PX,
|
|
6919
7185
|
MAX_HEADER_HEIGHT_PX,
|
|
7186
|
+
OptionalSegmentNode,
|
|
6920
7187
|
PT_BR_TRANSLATIONS,
|
|
7188
|
+
TOGGLE_OPTIONAL_SEGMENT_COMMAND,
|
|
6921
7189
|
VariableNode,
|
|
6922
7190
|
astExtension,
|
|
6923
7191
|
buildSavePayload,
|