@react-fabric/lexical 1.0.0-beta.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.
Files changed (136) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/dist/cjs/_virtual/prism.js +5 -0
  4. package/dist/cjs/editor/Lexical.module.css +1 -0
  5. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +432 -0
  6. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +21 -0
  7. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +27 -0
  8. package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +1139 -0
  9. package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +25 -0
  10. package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +50 -0
  11. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +245 -0
  12. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +19 -0
  13. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +18 -0
  14. package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +471 -0
  15. package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +17 -0
  16. package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +24 -0
  17. package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +1275 -0
  18. package/dist/cjs/node_modules/@lexical/list/LexicalList.js +29 -0
  19. package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +29 -0
  20. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +789 -0
  21. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +29 -0
  22. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +26 -0
  23. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +976 -0
  24. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +35 -0
  25. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +35 -0
  26. package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +2792 -0
  27. package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +23 -0
  28. package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +55 -0
  29. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +888 -0
  30. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +35 -0
  31. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +66 -0
  32. package/dist/cjs/node_modules/@mdi/js/mdi.js +8 -0
  33. package/dist/cjs/node_modules/prismjs/components/prism-c.js +82 -0
  34. package/dist/cjs/node_modules/prismjs/components/prism-clike.js +33 -0
  35. package/dist/cjs/node_modules/prismjs/components/prism-cpp.js +101 -0
  36. package/dist/cjs/node_modules/prismjs/components/prism-css.js +66 -0
  37. package/dist/cjs/node_modules/prismjs/components/prism-java.js +126 -0
  38. package/dist/cjs/node_modules/prismjs/components/prism-javascript.js +174 -0
  39. package/dist/cjs/node_modules/prismjs/components/prism-markdown.js +417 -0
  40. package/dist/cjs/node_modules/prismjs/components/prism-markup.js +188 -0
  41. package/dist/cjs/node_modules/prismjs/components/prism-objectivec.js +14 -0
  42. package/dist/cjs/node_modules/prismjs/components/prism-powershell.js +60 -0
  43. package/dist/cjs/node_modules/prismjs/components/prism-python.js +67 -0
  44. package/dist/cjs/node_modules/prismjs/components/prism-rust.js +130 -0
  45. package/dist/cjs/node_modules/prismjs/components/prism-sql.js +34 -0
  46. package/dist/cjs/node_modules/prismjs/components/prism-swift.js +150 -0
  47. package/dist/cjs/node_modules/prismjs/components/prism-typescript.js +62 -0
  48. package/dist/cjs/node_modules/prismjs/prism.js +1951 -0
  49. package/dist/cjs/packages/lexical/src/components/StickyComponent.js +221 -0
  50. package/dist/cjs/packages/lexical/src/editor/LexicalEditor.js +156 -0
  51. package/dist/cjs/packages/lexical/src/editor/theme.js +97 -0
  52. package/dist/cjs/packages/lexical/src/hooks/useFormatWatcher.js +451 -0
  53. package/dist/cjs/packages/lexical/src/index.js +7 -0
  54. package/dist/cjs/packages/lexical/src/nodes/StickyNode.js +218 -0
  55. package/dist/cjs/packages/lexical/src/plugins/DraggableBlockPlugin.js +314 -0
  56. package/dist/cjs/packages/lexical/src/plugins/StickyPlugin.js +20 -0
  57. package/dist/cjs/packages/lexical/src/plugins/ToolbarPlugin.js +258 -0
  58. package/dist/cjs/packages/lexical/src/utils/index.js +214 -0
  59. package/dist/esm/_virtual/prism.js +5 -0
  60. package/dist/esm/editor/Lexical.module.css +1 -0
  61. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +423 -0
  62. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +19 -0
  63. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +18 -0
  64. package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +1121 -0
  65. package/dist/esm/node_modules/@lexical/code/LexicalCode.js +21 -0
  66. package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +32 -0
  67. package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +244 -0
  68. package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +18 -0
  69. package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +17 -0
  70. package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +463 -0
  71. package/dist/esm/node_modules/@lexical/link/LexicalLink.js +17 -0
  72. package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +16 -0
  73. package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +1262 -0
  74. package/dist/esm/node_modules/@lexical/list/LexicalList.js +23 -0
  75. package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +16 -0
  76. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +781 -0
  77. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +23 -0
  78. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +18 -0
  79. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +957 -0
  80. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +26 -0
  81. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +16 -0
  82. package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +2754 -0
  83. package/dist/esm/node_modules/@lexical/table/LexicalTable.js +20 -0
  84. package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +17 -0
  85. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +839 -0
  86. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +26 -0
  87. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +17 -0
  88. package/dist/esm/node_modules/@mdi/js/mdi.js +7 -0
  89. package/dist/esm/node_modules/prismjs/components/prism-c.js +82 -0
  90. package/dist/esm/node_modules/prismjs/components/prism-clike.js +33 -0
  91. package/dist/esm/node_modules/prismjs/components/prism-cpp.js +101 -0
  92. package/dist/esm/node_modules/prismjs/components/prism-css.js +66 -0
  93. package/dist/esm/node_modules/prismjs/components/prism-java.js +126 -0
  94. package/dist/esm/node_modules/prismjs/components/prism-javascript.js +174 -0
  95. package/dist/esm/node_modules/prismjs/components/prism-markdown.js +417 -0
  96. package/dist/esm/node_modules/prismjs/components/prism-markup.js +188 -0
  97. package/dist/esm/node_modules/prismjs/components/prism-objectivec.js +14 -0
  98. package/dist/esm/node_modules/prismjs/components/prism-powershell.js +60 -0
  99. package/dist/esm/node_modules/prismjs/components/prism-python.js +67 -0
  100. package/dist/esm/node_modules/prismjs/components/prism-rust.js +130 -0
  101. package/dist/esm/node_modules/prismjs/components/prism-sql.js +34 -0
  102. package/dist/esm/node_modules/prismjs/components/prism-swift.js +150 -0
  103. package/dist/esm/node_modules/prismjs/components/prism-typescript.js +62 -0
  104. package/dist/esm/node_modules/prismjs/prism.js +1951 -0
  105. package/dist/esm/packages/lexical/src/components/StickyComponent.js +221 -0
  106. package/dist/esm/packages/lexical/src/editor/LexicalEditor.js +156 -0
  107. package/dist/esm/packages/lexical/src/editor/theme.js +97 -0
  108. package/dist/esm/packages/lexical/src/hooks/useFormatWatcher.js +450 -0
  109. package/dist/esm/packages/lexical/src/index.js +3 -0
  110. package/dist/esm/packages/lexical/src/nodes/StickyNode.js +216 -0
  111. package/dist/esm/packages/lexical/src/plugins/DraggableBlockPlugin.js +314 -0
  112. package/dist/esm/packages/lexical/src/plugins/StickyPlugin.js +20 -0
  113. package/dist/esm/packages/lexical/src/plugins/ToolbarPlugin.js +258 -0
  114. package/dist/esm/packages/lexical/src/utils/index.js +211 -0
  115. package/dist/types/components/StickyComponent.d.ts +9 -0
  116. package/dist/types/editor/LexicalEditor.d.ts +13 -0
  117. package/dist/types/editor/theme.d.ts +70 -0
  118. package/dist/types/hooks/useFormatWatcher.d.ts +40 -0
  119. package/dist/types/index.d.ts +1 -0
  120. package/dist/types/nodes/StickyNode.d.ts +35 -0
  121. package/dist/types/plugins/DraggableBlockPlugin.d.ts +3 -0
  122. package/dist/types/plugins/StickyPlugin.d.ts +1 -0
  123. package/dist/types/plugins/ToolbarPlugin.d.ts +1 -0
  124. package/dist/types/utils/index.d.ts +57 -0
  125. package/package.json +55 -0
  126. package/src/components/StickyComponent.tsx +271 -0
  127. package/src/editor/Lexical.module.css +218 -0
  128. package/src/editor/LexicalEditor.tsx +222 -0
  129. package/src/editor/theme.ts +93 -0
  130. package/src/hooks/useFormatWatcher.ts +504 -0
  131. package/src/index.ts +24 -0
  132. package/src/nodes/StickyNode.tsx +249 -0
  133. package/src/plugins/DraggableBlockPlugin.tsx +418 -0
  134. package/src/plugins/StickyPlugin.ts +38 -0
  135. package/src/plugins/ToolbarPlugin.tsx +279 -0
  136. package/src/utils/index.ts +303 -0
@@ -0,0 +1,279 @@
1
+ /*
2
+ * Marble NEXT
3
+ * @version : 4.0.0
4
+ * @copyright : 2023
5
+ * @license : Presight.ai
6
+ */
7
+
8
+ import {
9
+ Button,
10
+ ButtonGroup,
11
+ CoreIcons,
12
+ Divider,
13
+ Dropdown,
14
+ Icon,
15
+ Menu,
16
+ MenuItem,
17
+ ToggleButtonGroup,
18
+ Tooltip,
19
+ } from "@react-fabric/core";
20
+ import { ColorInput } from "@react-fabric/form";
21
+ import { getValue } from "@react-fabric/utilities";
22
+ import { FORMAT_ELEMENT_COMMAND, FORMAT_TEXT_COMMAND } from "lexical";
23
+ import { useMemo } from "react";
24
+ import { useTranslation } from "react-i18next";
25
+ import { useFormatWatcher } from "../hooks/useFormatWatcher";
26
+
27
+ const blockTypeToBlockName: KeyValue = {
28
+ bullet: "Bulleted List",
29
+ code: "Block",
30
+ h1: "Heading",
31
+ h2: "Sub Heading",
32
+ h6: "Comment",
33
+ number: "Numbered List",
34
+ paragraph: "Normal",
35
+ quote: "Quote",
36
+ };
37
+ const blockTypeToBlockIcon: KeyValue = {
38
+ bullet: CoreIcons.formatListBullet,
39
+ code: CoreIcons.formatCodeBlock,
40
+ h1: " ",
41
+ h2: " ",
42
+ paragraph: "",
43
+ h6: CoreIcons.formatCommentBlock,
44
+ number: CoreIcons.formatListNumber,
45
+ quote: CoreIcons.formatQuoteBlock,
46
+ };
47
+
48
+ const SIZES = Object.freeze([
49
+ "8",
50
+ "9",
51
+ "10",
52
+ "11",
53
+ "12",
54
+ "13",
55
+ "14",
56
+ "16",
57
+ "18",
58
+ "20",
59
+ "24",
60
+ "28",
61
+ "32",
62
+ "36",
63
+ "48",
64
+ "64",
65
+ "72",
66
+ ]);
67
+
68
+ const ButtonWrapper = ({ label, ...rest }: KeyValue) => (
69
+ <Tooltip content={label}>
70
+ <Button aria-label={label} tabIndex={-1} variant="link" {...rest} />
71
+ </Tooltip>
72
+ );
73
+
74
+ export const ToolbarPlugin = () => {
75
+ const watch = useFormatWatcher();
76
+ const { t } = useTranslation("lexical");
77
+
78
+ const textFormat = useMemo(() => {
79
+ const ret = [];
80
+ if (watch.isBold) ret.push("bold");
81
+ if (watch.isItalic) ret.push("italic");
82
+ if (watch.isUnderline) ret.push("underline");
83
+ return ret;
84
+ }, [watch]);
85
+
86
+ return (
87
+ <div className="border-b flex px-1 py-px">
88
+ <Dropdown>
89
+ <Button
90
+ variant="link"
91
+ tabIndex={-1}
92
+ altIcon={CoreIcons.caretDown}
93
+ icon={blockTypeToBlockIcon[watch.blockType]}
94
+ >
95
+ {blockTypeToBlockName[watch.blockType]}
96
+ </Button>
97
+ <Menu>
98
+ <MenuItem
99
+ icon={blockTypeToBlockIcon.h1}
100
+ label={blockTypeToBlockName.h1}
101
+ onClick={() => watch.formatHeading("h1")}
102
+ />
103
+ <MenuItem
104
+ icon={blockTypeToBlockIcon.h2}
105
+ label={blockTypeToBlockName.h2}
106
+ onClick={() => watch.formatHeading("h2")}
107
+ />
108
+ <MenuItem
109
+ icon={blockTypeToBlockIcon.paragraph}
110
+ label={blockTypeToBlockName.paragraph}
111
+ onClick={watch.formatNormal}
112
+ />
113
+ <MenuItem
114
+ icon={blockTypeToBlockIcon.quote}
115
+ label={blockTypeToBlockName.quote}
116
+ onClick={watch.formatQuote}
117
+ />
118
+ <MenuItem
119
+ icon={blockTypeToBlockIcon.code}
120
+ label={blockTypeToBlockName.code}
121
+ onClick={watch.formatBlock}
122
+ />
123
+ <MenuItem
124
+ icon={blockTypeToBlockIcon.h6}
125
+ label={blockTypeToBlockName.h6}
126
+ onClick={() => watch.formatHeading("h6")}
127
+ />
128
+ <MenuItem
129
+ icon={blockTypeToBlockIcon.bullet}
130
+ label={blockTypeToBlockName.bullet}
131
+ onClick={watch.formatBulletList}
132
+ />
133
+ <MenuItem
134
+ icon={blockTypeToBlockIcon.number}
135
+ label={blockTypeToBlockName.number}
136
+ onClick={watch.formatNumberedList}
137
+ />
138
+ </Menu>
139
+ </Dropdown>
140
+ <Divider vertical />
141
+ <div>
142
+ <Dropdown>
143
+ <Button variant="link" altIcon={CoreIcons.caretDown}>
144
+ {getValue(watch.fontSize + "", "16")}
145
+ </Button>
146
+ <Menu className="hide-icons">
147
+ {SIZES.map((size) => (
148
+ <MenuItem
149
+ key={size}
150
+ label={size}
151
+ active={watch.fontSize === size}
152
+ onClick={() => watch.onSizeChange(size)}
153
+ />
154
+ ))}
155
+ </Menu>
156
+ </Dropdown>
157
+ </div>
158
+ <Divider vertical />
159
+ <ButtonGroup variant="link">
160
+ <ButtonWrapper
161
+ value="start"
162
+ rtlFlip
163
+ label={t("tool.alignStart")}
164
+ icon={CoreIcons.alignLeft}
165
+ onClick={() =>
166
+ watch.activeEditor.dispatchCommand(FORMAT_ELEMENT_COMMAND, "start")
167
+ }
168
+ />
169
+ <ButtonWrapper
170
+ value="center"
171
+ label={t("tool.alignCenter")}
172
+ icon={CoreIcons.alignCenter}
173
+ onClick={() =>
174
+ watch.activeEditor.dispatchCommand(FORMAT_ELEMENT_COMMAND, "center")
175
+ }
176
+ />
177
+ <ButtonWrapper
178
+ value="end"
179
+ rtlFlip
180
+ label={t("tool.alignEnd")}
181
+ icon={CoreIcons.alignRight}
182
+ onClick={() =>
183
+ watch.activeEditor.dispatchCommand(FORMAT_ELEMENT_COMMAND, "end")
184
+ }
185
+ />
186
+ <ButtonWrapper
187
+ value="justify"
188
+ rtlFlip
189
+ label={t("tool.alignJustify")}
190
+ icon={CoreIcons.alignJustify}
191
+ onClick={() =>
192
+ watch.activeEditor.dispatchCommand(
193
+ FORMAT_ELEMENT_COMMAND,
194
+ "justify",
195
+ )
196
+ }
197
+ />
198
+ <ButtonWrapper
199
+ value="justify"
200
+ rtlFlip
201
+ label={t("tool.indentOut")}
202
+ icon={CoreIcons.formatIndentDecrease}
203
+ onClick={() => watch.outdent()}
204
+ />
205
+ <ButtonWrapper
206
+ value="justify"
207
+ rtlFlip
208
+ label={t("tool.identIn")}
209
+ icon={CoreIcons.formatIndentIncrease}
210
+ onClick={() => watch.indent()}
211
+ />
212
+ </ButtonGroup>
213
+ <Divider vertical />
214
+ <ToggleButtonGroup variant="link" value={textFormat}>
215
+ <ButtonWrapper
216
+ value="bold"
217
+ label={t("tool.bold")}
218
+ icon={CoreIcons.formatBold}
219
+ onClick={() =>
220
+ watch.activeEditor.dispatchCommand(FORMAT_TEXT_COMMAND, "bold")
221
+ }
222
+ />
223
+ <ButtonWrapper
224
+ value="italic"
225
+ label={t("tool.italic")}
226
+ icon={CoreIcons.formatItalic}
227
+ onClick={() =>
228
+ watch.activeEditor.dispatchCommand(FORMAT_TEXT_COMMAND, "italic")
229
+ }
230
+ />
231
+ <ButtonWrapper
232
+ value="underline"
233
+ label={t("tool.underline")}
234
+ icon={CoreIcons.formatUnderline}
235
+ onClick={() =>
236
+ watch.activeEditor.dispatchCommand(FORMAT_TEXT_COMMAND, "underline")
237
+ }
238
+ />
239
+ <ButtonWrapper
240
+ value="strikethrough"
241
+ label={t("tool.strikethrough")}
242
+ icon={CoreIcons.formatStrikethrough}
243
+ onClick={() =>
244
+ watch.activeEditor.dispatchCommand(
245
+ FORMAT_TEXT_COMMAND,
246
+ "strikethrough",
247
+ )
248
+ }
249
+ />
250
+ </ToggleButtonGroup>
251
+ <Divider vertical />
252
+ <ColorInput
253
+ {...{ noOutline: true }}
254
+ value={watch.fontColor}
255
+ onChange={(v = "#000") => watch.onFontColorSelect(v)}
256
+ decorateStart={<Icon icon={CoreIcons.formatText} />}
257
+ />
258
+ <ColorInput
259
+ {...{ noOutline: true }}
260
+ value={watch.bgColor}
261
+ onChange={(v = "#fff") => watch.onBgColorSelect(v)}
262
+ decorateStart={<Icon icon={CoreIcons.formatFill} />}
263
+ />
264
+ <ButtonWrapper
265
+ value="hilight"
266
+ label={t("tool.hilight")}
267
+ icon={CoreIcons.formatHilight}
268
+ onClick={() => watch.onBgColorSelect("#fde68a")}
269
+ />
270
+ <ButtonWrapper
271
+ value="hilight"
272
+ label={t("tool.clear")}
273
+ icon={CoreIcons.formatClear}
274
+ onClick={() => watch.clearFormatting()}
275
+ />
276
+ <Divider />
277
+ </div>
278
+ );
279
+ };
@@ -0,0 +1,303 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import {
25
+ isArray,
26
+ isObject,
27
+ isString,
28
+ matchString,
29
+ } from "@react-fabric/utilities";
30
+
31
+ export function isHTMLElement(x: unknown): x is HTMLElement {
32
+ return x instanceof HTMLElement;
33
+ }
34
+
35
+ export class Point {
36
+ private readonly _x: number;
37
+ private readonly _y: number;
38
+
39
+ constructor(x: number, y: number) {
40
+ this._x = x;
41
+ this._y = y;
42
+ }
43
+
44
+ get x(): number {
45
+ return this._x;
46
+ }
47
+
48
+ get y(): number {
49
+ return this._y;
50
+ }
51
+
52
+ public equals({ x, y }: Point): boolean {
53
+ return this.x === x && this.y === y;
54
+ }
55
+
56
+ public calcDeltaXTo({ x }: Point): number {
57
+ return this.x - x;
58
+ }
59
+
60
+ public calcDeltaYTo({ y }: Point): number {
61
+ return this.y - y;
62
+ }
63
+
64
+ public calcHorizontalDistanceTo(point: Point): number {
65
+ return Math.abs(this.calcDeltaXTo(point));
66
+ }
67
+
68
+ public calcVerticalDistance(point: Point): number {
69
+ return Math.abs(this.calcDeltaYTo(point));
70
+ }
71
+
72
+ public calcDistanceTo(point: Point): number {
73
+ return Math.sqrt(
74
+ Math.pow(this.calcDeltaXTo(point), 2) +
75
+ Math.pow(this.calcDeltaYTo(point), 2),
76
+ );
77
+ }
78
+ }
79
+
80
+ export function isPoint(x: unknown): x is Point {
81
+ return x instanceof Point;
82
+ }
83
+
84
+ interface ContainsPointReturn {
85
+ result: boolean;
86
+ reason: {
87
+ isOnTopSide: boolean;
88
+ isOnBottomSide: boolean;
89
+ isOnLeftSide: boolean;
90
+ isOnRightSide: boolean;
91
+ };
92
+ }
93
+
94
+ export class Rect {
95
+ private readonly _left: number;
96
+ private readonly _top: number;
97
+ private readonly _right: number;
98
+ private readonly _bottom: number;
99
+
100
+ constructor(left: number, top: number, right: number, bottom: number) {
101
+ const [physicTop, physicBottom] =
102
+ top <= bottom ? [top, bottom] : [bottom, top];
103
+
104
+ const [physicLeft, physicRight] =
105
+ left <= right ? [left, right] : [right, left];
106
+
107
+ this._top = physicTop;
108
+ this._right = physicRight;
109
+ this._left = physicLeft;
110
+ this._bottom = physicBottom;
111
+ }
112
+
113
+ get top(): number {
114
+ return this._top;
115
+ }
116
+
117
+ get right(): number {
118
+ return this._right;
119
+ }
120
+
121
+ get bottom(): number {
122
+ return this._bottom;
123
+ }
124
+
125
+ get left(): number {
126
+ return this._left;
127
+ }
128
+
129
+ get width(): number {
130
+ return Math.abs(this._left - this._right);
131
+ }
132
+
133
+ get height(): number {
134
+ return Math.abs(this._bottom - this._top);
135
+ }
136
+
137
+ public equals({ top, left, bottom, right }: Rect): boolean {
138
+ return (
139
+ top === this._top &&
140
+ bottom === this._bottom &&
141
+ left === this._left &&
142
+ right === this._right
143
+ );
144
+ }
145
+
146
+ public contains({ x, y }: Point): ContainsPointReturn;
147
+ public contains({ top, left, bottom, right }: Rect): boolean;
148
+ public contains(target: Point | Rect): boolean | ContainsPointReturn {
149
+ if (isPoint(target)) {
150
+ const { x, y } = target;
151
+
152
+ const isOnTopSide = y < this._top;
153
+ const isOnBottomSide = y > this._bottom;
154
+ const isOnLeftSide = x < this._left;
155
+ const isOnRightSide = x > this._right;
156
+
157
+ const result =
158
+ !isOnTopSide && !isOnBottomSide && !isOnLeftSide && !isOnRightSide;
159
+
160
+ return {
161
+ reason: {
162
+ isOnBottomSide,
163
+ isOnLeftSide,
164
+ isOnRightSide,
165
+ isOnTopSide,
166
+ },
167
+ result,
168
+ };
169
+ } else {
170
+ const { top, left, bottom, right } = target;
171
+
172
+ return (
173
+ top >= this._top &&
174
+ top <= this._bottom &&
175
+ bottom >= this._top &&
176
+ bottom <= this._bottom &&
177
+ left >= this._left &&
178
+ left <= this._right &&
179
+ right >= this._left &&
180
+ right <= this._right
181
+ );
182
+ }
183
+ }
184
+
185
+ public intersectsWith(rect: Rect): boolean {
186
+ const { left: x1, top: y1, width: w1, height: h1 } = rect;
187
+ const { left: x2, top: y2, width: w2, height: h2 } = this;
188
+ const maxX = x1 + w1 >= x2 + w2 ? x1 + w1 : x2 + w2;
189
+ const maxY = y1 + h1 >= y2 + h2 ? y1 + h1 : y2 + h2;
190
+ const minX = x1 <= x2 ? x1 : x2;
191
+ const minY = y1 <= y2 ? y1 : y2;
192
+ return maxX - minX <= w1 + w2 && maxY - minY <= h1 + h2;
193
+ }
194
+
195
+ public generateNewRect({
196
+ left = this.left,
197
+ top = this.top,
198
+ right = this.right,
199
+ bottom = this.bottom,
200
+ }): Rect {
201
+ return new Rect(left, top, right, bottom);
202
+ }
203
+
204
+ static fromLTRB(
205
+ left: number,
206
+ top: number,
207
+ right: number,
208
+ bottom: number,
209
+ ): Rect {
210
+ return new Rect(left, top, right, bottom);
211
+ }
212
+
213
+ static fromLWTH(
214
+ left: number,
215
+ width: number,
216
+ top: number,
217
+ height: number,
218
+ ): Rect {
219
+ return new Rect(left, top, left + width, top + height);
220
+ }
221
+
222
+ static fromPoints(startPoint: Point, endPoint: Point): Rect {
223
+ const { y: top, x: left } = startPoint;
224
+ const { y: bottom, x: right } = endPoint;
225
+ return Rect.fromLTRB(left, top, right, bottom);
226
+ }
227
+
228
+ static fromDOM(dom: HTMLElement): Rect {
229
+ const { top, width, left, height } = dom.getBoundingClientRect();
230
+ return Rect.fromLWTH(left, width, top, height);
231
+ }
232
+ }
233
+
234
+ export const convertLatLng = (lonLat: AnyObject) => {
235
+ if (
236
+ (isArray(lonLat) && lonLat.length === 2) ||
237
+ (isArray(lonLat) && lonLat.length === 3)
238
+ ) {
239
+ return [Number(lonLat[0]), Number(lonLat[1])];
240
+ }
241
+
242
+ if (isObject(lonLat)) {
243
+ if ("longitude" in lonLat) {
244
+ return [lonLat.longitude, lonLat.latitude];
245
+ }
246
+ return ["lon" in lonLat ? lonLat.lon : lonLat.lng, lonLat.lat];
247
+ }
248
+
249
+ if (isString(lonLat)) {
250
+ const [lat, lon] = lonLat.split(",");
251
+ return [+lon, +lat];
252
+ }
253
+
254
+ throw Error("Invalid geo cooredinates");
255
+ };
256
+
257
+ export const convertToDMS = (coord: number, isLat = false) => {
258
+ const degree = Math.floor(coord);
259
+ const hour = (coord - degree) * 60;
260
+ const minute = (hour - Math.floor(hour)) * 60;
261
+
262
+ return `${degree}˚${Math.floor(hour)}'${minute.toFixed(2)}"${
263
+ coord < 0 ? (isLat ? "S" : "W") : isLat ? "N" : "E"
264
+ }`;
265
+ };
266
+
267
+ export const getLocationAsText = (latlon: AnyObject) => {
268
+ const [lon, lat] = convertLatLng(latlon);
269
+ return `${convertToDMS(lat, true)}, ${convertToDMS(lon)}`;
270
+ };
271
+
272
+ export const getLocationUrl = (latlon: AnyObject) => {
273
+ const [lon, lat] = convertLatLng(latlon);
274
+ return `https://google.com/maps/?q=${lat},${lon}`;
275
+ };
276
+
277
+ export const bookmarksFilterHookMatcher = (
278
+ bookmark: KeyValue,
279
+ query: string,
280
+ ) => {
281
+ return (
282
+ matchString(bookmark.subtype ?? bookmark.type, query) ||
283
+ matchString(bookmark.fileName ?? bookmark.name, query) ||
284
+ ("message" in bookmark && matchString(bookmark.message ?? "", query)) ||
285
+ ("title" in bookmark && matchString(bookmark.title ?? "", query)) ||
286
+ ("url" in bookmark && matchString(bookmark.url ?? "", query)) ||
287
+ ("category" in bookmark && matchString(bookmark.category ?? "", query)) ||
288
+ ("device" in bookmark && matchString(bookmark.device ?? "", query)) ||
289
+ ("browser" in bookmark && matchString(bookmark.browser ?? "", query)) ||
290
+ ("from" in bookmark &&
291
+ bookmark.from.phone &&
292
+ matchString(bookmark.from.phone ?? "", query)) ||
293
+ ("from" in bookmark &&
294
+ bookmark.from.name &&
295
+ matchString(bookmark.from.name ?? "", query)) ||
296
+ ("to" in bookmark &&
297
+ bookmark.to.phone &&
298
+ matchString(bookmark.to.phone ?? "", query)) ||
299
+ ("to" in bookmark &&
300
+ bookmark.to.name &&
301
+ matchString(bookmark.to.name ?? "", query))
302
+ );
303
+ };