]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/lexical/core/LexicalConstants.ts
Lexical: Imported core lexical libs
[bookstack] / resources / js / wysiwyg / lexical / core / LexicalConstants.ts
1 /**
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  *
7  */
8
9 import type {ElementFormatType} from './nodes/LexicalElementNode';
10 import type {
11   TextDetailType,
12   TextFormatType,
13   TextModeType,
14 } from './nodes/LexicalTextNode';
15
16 import {
17   IS_APPLE_WEBKIT,
18   IS_FIREFOX,
19   IS_IOS,
20   IS_SAFARI,
21 } from 'lexical/shared/environment';
22
23 // DOM
24 export const DOM_ELEMENT_TYPE = 1;
25 export const DOM_TEXT_TYPE = 3;
26
27 // Reconciling
28 export const NO_DIRTY_NODES = 0;
29 export const HAS_DIRTY_NODES = 1;
30 export const FULL_RECONCILE = 2;
31
32 // Text node modes
33 export const IS_NORMAL = 0;
34 export const IS_TOKEN = 1;
35 export const IS_SEGMENTED = 2;
36 // IS_INERT = 3
37
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;
47
48 export const IS_ALL_FORMATTING =
49   IS_BOLD |
50   IS_ITALIC |
51   IS_STRIKETHROUGH |
52   IS_UNDERLINE |
53   IS_CODE |
54   IS_SUBSCRIPT |
55   IS_SUPERSCRIPT |
56   IS_HIGHLIGHT;
57
58 // Text node details
59 export const IS_DIRECTIONLESS = 1;
60 export const IS_UNMERGEABLE = 1 << 1;
61
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;
69
70 // Reconciliation
71 export const NON_BREAKING_SPACE = '\u00A0';
72 const ZERO_WIDTH_SPACE = '\u200b';
73
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
78     ? NON_BREAKING_SPACE
79     : ZERO_WIDTH_SPACE;
80 export const DOUBLE_LINE_BREAK = '\n\n';
81
82 // For FF, we need to use a non-breaking space, or it gets composition
83 // in a stuck state.
84 export const COMPOSITION_START_CHAR: string = IS_FIREFOX
85   ? NON_BREAKING_SPACE
86   : COMPOSITION_SUFFIX;
87 const RTL = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC';
88 const LTR =
89   'A-Za-z\u00C0-\u00D6\u00D8-\u00F6' +
90   '\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF\u200E\u2C00-\uFB1C' +
91   '\uFE00-\uFE6F\uFEFD-\uFFFF';
92
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 + ']');
97
98 export const TEXT_TYPE_TO_FORMAT: Record<TextFormatType | string, number> = {
99   bold: IS_BOLD,
100   code: IS_CODE,
101   highlight: IS_HIGHLIGHT,
102   italic: IS_ITALIC,
103   strikethrough: IS_STRIKETHROUGH,
104   subscript: IS_SUBSCRIPT,
105   superscript: IS_SUPERSCRIPT,
106   underline: IS_UNDERLINE,
107 };
108
109 export const DETAIL_TYPE_TO_DETAIL: Record<TextDetailType | string, number> = {
110   directionless: IS_DIRECTIONLESS,
111   unmergeable: IS_UNMERGEABLE,
112 };
113
114 export const ELEMENT_TYPE_TO_FORMAT: Record<
115   Exclude<ElementFormatType, ''>,
116   number
117 > = {
118   center: IS_ALIGN_CENTER,
119   end: IS_ALIGN_END,
120   justify: IS_ALIGN_JUSTIFY,
121   left: IS_ALIGN_LEFT,
122   right: IS_ALIGN_RIGHT,
123   start: IS_ALIGN_START,
124 };
125
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',
133 };
134
135 export const TEXT_MODE_TO_TYPE: Record<TextModeType, 0 | 1 | 2> = {
136   normal: IS_NORMAL,
137   segmented: IS_SEGMENTED,
138   token: IS_TOKEN,
139 };
140
141 export const TEXT_TYPE_TO_MODE: Record<number, TextModeType> = {
142   [IS_NORMAL]: 'normal',
143   [IS_SEGMENTED]: 'segmented',
144   [IS_TOKEN]: 'token',
145 };