2 * Copyright (c) Meta Platforms, Inc. and affiliates.
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
9 import type {ElementFormatType} from './nodes/LexicalElementNode';
14 } from './nodes/LexicalTextNode';
21 } from 'lexical/shared/environment';
24 export const DOM_ELEMENT_TYPE = 1;
25 export const DOM_TEXT_TYPE = 3;
28 export const NO_DIRTY_NODES = 0;
29 export const HAS_DIRTY_NODES = 1;
30 export const FULL_RECONCILE = 2;
33 export const IS_NORMAL = 0;
34 export const IS_TOKEN = 1;
35 export const IS_SEGMENTED = 2;
38 // Text node formatting
39 export const IS_BOLD = 1;
40 export const IS_ITALIC = 1 << 1;
41 export const IS_STRIKETHROUGH = 1 << 2;
42 export const IS_UNDERLINE = 1 << 3;
43 export const IS_CODE = 1 << 4;
44 export const IS_SUBSCRIPT = 1 << 5;
45 export const IS_SUPERSCRIPT = 1 << 6;
46 export const IS_HIGHLIGHT = 1 << 7;
48 export const IS_ALL_FORMATTING =
59 export const IS_DIRECTIONLESS = 1;
60 export const IS_UNMERGEABLE = 1 << 1;
62 // Element node formatting
63 export const IS_ALIGN_LEFT = 1;
64 export const IS_ALIGN_CENTER = 2;
65 export const IS_ALIGN_RIGHT = 3;
66 export const IS_ALIGN_JUSTIFY = 4;
67 export const IS_ALIGN_START = 5;
68 export const IS_ALIGN_END = 6;
71 export const NON_BREAKING_SPACE = '\u00A0';
72 const ZERO_WIDTH_SPACE = '\u200b';
74 // For iOS/Safari we use a non breaking space, otherwise the cursor appears
75 // overlapping the composed text.
76 export const COMPOSITION_SUFFIX: string =
77 IS_SAFARI || IS_IOS || IS_APPLE_WEBKIT
80 export const DOUBLE_LINE_BREAK = '\n\n';
82 // For FF, we need to use a non-breaking space, or it gets composition
84 export const COMPOSITION_START_CHAR: string = IS_FIREFOX
87 const RTL = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC';
89 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6' +
90 '\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF\u200E\u2C00-\uFB1C' +
91 '\uFE00-\uFE6F\uFEFD-\uFFFF';
93 // eslint-disable-next-line no-misleading-character-class
94 export const RTL_REGEX = new RegExp('^[^' + LTR + ']*[' + RTL + ']');
95 // eslint-disable-next-line no-misleading-character-class
96 export const LTR_REGEX = new RegExp('^[^' + RTL + ']*[' + LTR + ']');
98 export const TEXT_TYPE_TO_FORMAT: Record<TextFormatType | string, number> = {
101 highlight: IS_HIGHLIGHT,
103 strikethrough: IS_STRIKETHROUGH,
104 subscript: IS_SUBSCRIPT,
105 superscript: IS_SUPERSCRIPT,
106 underline: IS_UNDERLINE,
109 export const DETAIL_TYPE_TO_DETAIL: Record<TextDetailType | string, number> = {
110 directionless: IS_DIRECTIONLESS,
111 unmergeable: IS_UNMERGEABLE,
114 export const ELEMENT_TYPE_TO_FORMAT: Record<
115 Exclude<ElementFormatType, ''>,
118 center: IS_ALIGN_CENTER,
120 justify: IS_ALIGN_JUSTIFY,
122 right: IS_ALIGN_RIGHT,
123 start: IS_ALIGN_START,
126 export const ELEMENT_FORMAT_TO_TYPE: Record<number, ElementFormatType> = {
127 [IS_ALIGN_CENTER]: 'center',
128 [IS_ALIGN_END]: 'end',
129 [IS_ALIGN_JUSTIFY]: 'justify',
130 [IS_ALIGN_LEFT]: 'left',
131 [IS_ALIGN_RIGHT]: 'right',
132 [IS_ALIGN_START]: 'start',
135 export const TEXT_MODE_TO_TYPE: Record<TextModeType, 0 | 1 | 2> = {
137 segmented: IS_SEGMENTED,
141 export const TEXT_TYPE_TO_MODE: Record<number, TextModeType> = {
142 [IS_NORMAL]: 'normal',
143 [IS_SEGMENTED]: 'segmented',