@nordcraft/runtime 1.0.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 (168) hide show
  1. package/README.md +5 -0
  2. package/dist/api/createAPI.d.ts +20 -0
  3. package/dist/api/createAPI.js +319 -0
  4. package/dist/api/createAPI.js.map +1 -0
  5. package/dist/api/createAPIv2.d.ts +7 -0
  6. package/dist/api/createAPIv2.js +686 -0
  7. package/dist/api/createAPIv2.js.map +1 -0
  8. package/dist/components/createComponent.d.ts +13 -0
  9. package/dist/components/createComponent.js +216 -0
  10. package/dist/components/createComponent.js.map +1 -0
  11. package/dist/components/createElement.d.ts +3 -0
  12. package/dist/components/createElement.js +208 -0
  13. package/dist/components/createElement.js.map +1 -0
  14. package/dist/components/createNode.d.ts +22 -0
  15. package/dist/components/createNode.js +272 -0
  16. package/dist/components/createNode.js.map +1 -0
  17. package/dist/components/createSlot.d.ts +3 -0
  18. package/dist/components/createSlot.js +49 -0
  19. package/dist/components/createSlot.js.map +1 -0
  20. package/dist/components/createText.d.ts +23 -0
  21. package/dist/components/createText.js +68 -0
  22. package/dist/components/createText.js.map +1 -0
  23. package/dist/components/createText.test.d.ts +1 -0
  24. package/dist/components/createText.test.js +113 -0
  25. package/dist/components/createText.test.js.map +1 -0
  26. package/dist/components/renderComponent.d.ts +34 -0
  27. package/dist/components/renderComponent.js +66 -0
  28. package/dist/components/renderComponent.js.map +1 -0
  29. package/dist/context/isContextProvider.d.ts +2 -0
  30. package/dist/context/isContextProvider.js +5 -0
  31. package/dist/context/isContextProvider.js.map +1 -0
  32. package/dist/context/subscribeToContext.d.ts +4 -0
  33. package/dist/context/subscribeToContext.js +93 -0
  34. package/dist/context/subscribeToContext.js.map +1 -0
  35. package/dist/custom-components/components.d.ts +1 -0
  36. package/dist/custom-components/components.js +2 -0
  37. package/dist/custom-components/components.js.map +1 -0
  38. package/dist/custom-components/toddle-portal.d.ts +6 -0
  39. package/dist/custom-components/toddle-portal.js +20 -0
  40. package/dist/custom-components/toddle-portal.js.map +1 -0
  41. package/dist/custom-element/ToddleComponent.d.ts +37 -0
  42. package/dist/custom-element/ToddleComponent.js +244 -0
  43. package/dist/custom-element/ToddleComponent.js.map +1 -0
  44. package/dist/custom-element/defineComponents.d.ts +26 -0
  45. package/dist/custom-element/defineComponents.js +42 -0
  46. package/dist/custom-element/defineComponents.js.map +1 -0
  47. package/dist/custom-element.main.d.ts +3 -0
  48. package/dist/custom-element.main.esm.js +266 -0
  49. package/dist/custom-element.main.esm.js.map +7 -0
  50. package/dist/custom-element.main.js +14 -0
  51. package/dist/custom-element.main.js.map +1 -0
  52. package/dist/debug/logState.d.ts +4 -0
  53. package/dist/debug/logState.js +19 -0
  54. package/dist/debug/logState.js.map +1 -0
  55. package/dist/editor/drag-drop/dragEnded.d.ts +2 -0
  56. package/dist/editor/drag-drop/dragEnded.js +56 -0
  57. package/dist/editor/drag-drop/dragEnded.js.map +1 -0
  58. package/dist/editor/drag-drop/dragMove.d.ts +3 -0
  59. package/dist/editor/drag-drop/dragMove.js +74 -0
  60. package/dist/editor/drag-drop/dragMove.js.map +1 -0
  61. package/dist/editor/drag-drop/dragReorder.d.ts +3 -0
  62. package/dist/editor/drag-drop/dragReorder.js +92 -0
  63. package/dist/editor/drag-drop/dragReorder.js.map +1 -0
  64. package/dist/editor/drag-drop/dragStarted.d.ts +9 -0
  65. package/dist/editor/drag-drop/dragStarted.js +100 -0
  66. package/dist/editor/drag-drop/dragStarted.js.map +1 -0
  67. package/dist/editor/drag-drop/dropHighlight.d.ts +16 -0
  68. package/dist/editor/drag-drop/dropHighlight.js +50 -0
  69. package/dist/editor/drag-drop/dropHighlight.js.map +1 -0
  70. package/dist/editor/drag-drop/getInsertAreas.d.ts +20 -0
  71. package/dist/editor/drag-drop/getInsertAreas.js +220 -0
  72. package/dist/editor/drag-drop/getInsertAreas.js.map +1 -0
  73. package/dist/editor-preview.main.d.ts +19 -0
  74. package/dist/editor-preview.main.js +1303 -0
  75. package/dist/editor-preview.main.js.map +1 -0
  76. package/dist/events/handleAction.d.ts +3 -0
  77. package/dist/events/handleAction.js +307 -0
  78. package/dist/events/handleAction.js.map +1 -0
  79. package/dist/page.main.d.ts +7 -0
  80. package/dist/page.main.esm.js +8 -0
  81. package/dist/page.main.esm.js.map +7 -0
  82. package/dist/page.main.js +395 -0
  83. package/dist/page.main.js.map +1 -0
  84. package/dist/signal/signal.d.ts +19 -0
  85. package/dist/signal/signal.js +65 -0
  86. package/dist/signal/signal.js.map +1 -0
  87. package/dist/styles/style.d.ts +4 -0
  88. package/dist/styles/style.js +196 -0
  89. package/dist/styles/style.js.map +1 -0
  90. package/dist/utils/BatchQueue.d.ts +10 -0
  91. package/dist/utils/BatchQueue.js +25 -0
  92. package/dist/utils/BatchQueue.js.map +1 -0
  93. package/dist/utils/createFormulaCache.d.ts +3 -0
  94. package/dist/utils/createFormulaCache.js +81 -0
  95. package/dist/utils/createFormulaCache.js.map +1 -0
  96. package/dist/utils/findNearestLine.d.ts +13 -0
  97. package/dist/utils/findNearestLine.js +74 -0
  98. package/dist/utils/findNearestLine.js.map +1 -0
  99. package/dist/utils/findNearestLine.test.d.ts +1 -0
  100. package/dist/utils/findNearestLine.test.js +59 -0
  101. package/dist/utils/findNearestLine.test.js.map +1 -0
  102. package/dist/utils/getDragData.d.ts +1 -0
  103. package/dist/utils/getDragData.js +10 -0
  104. package/dist/utils/getDragData.js.map +1 -0
  105. package/dist/utils/getElementTagName.d.ts +3 -0
  106. package/dist/utils/getElementTagName.js +7 -0
  107. package/dist/utils/getElementTagName.js.map +1 -0
  108. package/dist/utils/nodes.d.ts +21 -0
  109. package/dist/utils/nodes.js +89 -0
  110. package/dist/utils/nodes.js.map +1 -0
  111. package/dist/utils/omitStyle.d.ts +2 -0
  112. package/dist/utils/omitStyle.js +13 -0
  113. package/dist/utils/omitStyle.js.map +1 -0
  114. package/dist/utils/rectHasPoint.d.ts +2 -0
  115. package/dist/utils/rectHasPoint.js +4 -0
  116. package/dist/utils/rectHasPoint.js.map +1 -0
  117. package/dist/utils/setAttribute.d.ts +4 -0
  118. package/dist/utils/setAttribute.js +57 -0
  119. package/dist/utils/setAttribute.js.map +1 -0
  120. package/dist/utils/tryStartViewTransition.d.ts +5 -0
  121. package/dist/utils/tryStartViewTransition.js +14 -0
  122. package/dist/utils/tryStartViewTransition.js.map +1 -0
  123. package/dist/utils/url.d.ts +2 -0
  124. package/dist/utils/url.js +36 -0
  125. package/dist/utils/url.js.map +1 -0
  126. package/package.json +25 -0
  127. package/src/api/createAPI.ts +375 -0
  128. package/src/api/createAPIv2.ts +931 -0
  129. package/src/components/createComponent.ts +280 -0
  130. package/src/components/createElement.ts +240 -0
  131. package/src/components/createNode.ts +381 -0
  132. package/src/components/createSlot.ts +61 -0
  133. package/src/components/createText.test.ts +117 -0
  134. package/src/components/createText.ts +104 -0
  135. package/src/components/renderComponent.ts +145 -0
  136. package/src/context/isContextProvider.ts +12 -0
  137. package/src/context/subscribeToContext.ts +135 -0
  138. package/src/custom-components/components.ts +1 -0
  139. package/src/custom-components/toddle-portal.ts +19 -0
  140. package/src/custom-element/ToddleComponent.ts +315 -0
  141. package/src/custom-element/defineComponents.ts +65 -0
  142. package/src/custom-element.main.ts +24 -0
  143. package/src/debug/logState.ts +30 -0
  144. package/src/editor/drag-drop/dragEnded.ts +75 -0
  145. package/src/editor/drag-drop/dragMove.ts +95 -0
  146. package/src/editor/drag-drop/dragReorder.ts +137 -0
  147. package/src/editor/drag-drop/dragStarted.ts +145 -0
  148. package/src/editor/drag-drop/dropHighlight.ts +82 -0
  149. package/src/editor/drag-drop/getInsertAreas.ts +235 -0
  150. package/src/editor/types.d.ts +36 -0
  151. package/src/editor-preview.main.ts +1782 -0
  152. package/src/events/handleAction.ts +387 -0
  153. package/src/page.main.ts +489 -0
  154. package/src/signal/signal.ts +74 -0
  155. package/src/styles/style.ts +254 -0
  156. package/src/types.d.ts +93 -0
  157. package/src/utils/BatchQueue.ts +24 -0
  158. package/src/utils/createFormulaCache.ts +96 -0
  159. package/src/utils/findNearestLine.test.ts +65 -0
  160. package/src/utils/findNearestLine.ts +92 -0
  161. package/src/utils/getDragData.ts +11 -0
  162. package/src/utils/getElementTagName.ts +14 -0
  163. package/src/utils/nodes.ts +125 -0
  164. package/src/utils/omitStyle.ts +19 -0
  165. package/src/utils/rectHasPoint.ts +5 -0
  166. package/src/utils/setAttribute.ts +56 -0
  167. package/src/utils/tryStartViewTransition.ts +32 -0
  168. package/src/utils/url.ts +45 -0
@@ -0,0 +1,235 @@
1
+ import { getDOMNodeFromNodeId } from '../../editor-preview.main'
2
+ import type { InsertArea } from '../types'
3
+
4
+ /**
5
+ * Somewhat convoluted function to calculate all possible drop insertion areas, as lines between elements.
6
+ *
7
+ * Drop locations follows the following rules:
8
+ * - All lines for a single container are either horizontal or vertical (block or inline layout)
9
+ * - If the next sibling of an element follows the expected layout (not wrapped) a line is drawn between the two (taking gap/margin into consideration),
10
+ * - If the next sibling is wrapped, a line is drawn both after and before the next element. Both lines inserts the dragged element at the same index.
11
+ */
12
+ export function getInsertAreas() {
13
+ const insertAreas: Array<InsertArea> = []
14
+ Array.from(document.querySelectorAll('[data-id]:not([data-component])'))
15
+ .filter((e) => e.getAttribute('data-id')?.includes(')') === false)
16
+ .map((e) => e.getAttribute('data-id'))
17
+ .forEach((id) => {
18
+ const element = getDOMNodeFromNodeId(id)
19
+ if (!element) {
20
+ // eslint-disable-next-line no-console
21
+ console.warn(`Element with path ${id} not found`)
22
+ return
23
+ }
24
+
25
+ const rect = element.getBoundingClientRect()
26
+ const parent = element.parentElement
27
+ if (!parent) {
28
+ return
29
+ }
30
+
31
+ const siblings = Array.from(parent.children).filter(
32
+ (c) =>
33
+ c.hasAttribute('data-node-id') &&
34
+ c.getAttribute('data-node-id')?.endsWith(')') === false,
35
+ )
36
+ const index = siblings.indexOf(element)
37
+ const nextRect = siblings[index + 1]?.getBoundingClientRect()
38
+ const prevRect = siblings[index - 1]?.getBoundingClientRect()
39
+ const isBlockLayout =
40
+ siblings.length > 1 &&
41
+ siblings
42
+ .map((c) => c.getBoundingClientRect())
43
+ .every(
44
+ (r, i, rects) =>
45
+ i === 0 ||
46
+ r.width + r.height === 0 ||
47
+ rects[i - 1].bottom <= r.top,
48
+ )
49
+
50
+ if (isBlockLayout) {
51
+ if (prevRect) {
52
+ if (prevRect.bottom >= rect.top) {
53
+ insertAreas.push({
54
+ layout: 'block',
55
+ parent,
56
+ index,
57
+ center: {
58
+ x: rect.left + rect.width / 2,
59
+ y: rect.top,
60
+ },
61
+ size: rect.width,
62
+ direction: -1,
63
+ })
64
+ }
65
+ } else if (siblings.length > 0) {
66
+ insertAreas.push({
67
+ layout: 'block',
68
+ parent,
69
+ index,
70
+ center: {
71
+ x: rect.left + rect.width / 2,
72
+ y: rect.top,
73
+ },
74
+ size: rect.width,
75
+ direction: -1,
76
+ })
77
+ }
78
+
79
+ if (nextRect) {
80
+ if (nextRect.top > rect.bottom) {
81
+ insertAreas.push({
82
+ layout: 'block',
83
+ parent,
84
+ index: index + 1,
85
+ center: {
86
+ x: rect.left + rect.width / 2,
87
+ y: (rect.bottom + nextRect.top) / 2,
88
+ },
89
+ size: rect.width,
90
+ direction: 1,
91
+ })
92
+ } else {
93
+ insertAreas.push({
94
+ layout: 'block',
95
+ parent,
96
+ index: index + 1,
97
+ center: {
98
+ x: rect.left + rect.width / 2,
99
+ y: rect.bottom,
100
+ },
101
+ size: rect.width,
102
+ direction: 1,
103
+ })
104
+ }
105
+ } else if (siblings.length > 0) {
106
+ insertAreas.push({
107
+ layout: 'block',
108
+ parent,
109
+ index: index + 1,
110
+ center: {
111
+ x: rect.left + rect.width / 2,
112
+ y: rect.bottom,
113
+ },
114
+ size: rect.width,
115
+ direction: 1,
116
+ })
117
+ }
118
+ } else {
119
+ if (prevRect) {
120
+ if (prevRect.right >= rect.left) {
121
+ insertAreas.push({
122
+ layout: 'inline',
123
+ parent,
124
+ index,
125
+ center: {
126
+ x: rect.left,
127
+ y: rect.top + rect.height / 2,
128
+ },
129
+ size: rect.height,
130
+ direction: -1,
131
+ })
132
+ }
133
+ } else if (siblings.length > 0) {
134
+ insertAreas.push({
135
+ layout: 'inline',
136
+ parent,
137
+ index,
138
+ center: {
139
+ x: rect.left,
140
+ y: rect.top + rect.height / 2,
141
+ },
142
+ size: rect.height,
143
+ direction: -1,
144
+ })
145
+ }
146
+
147
+ if (nextRect) {
148
+ if (nextRect.left > rect.right) {
149
+ insertAreas.push({
150
+ layout: 'inline',
151
+ parent,
152
+ index: index + 1,
153
+ center: {
154
+ x: (rect.right + nextRect.left) / 2,
155
+ y: nextRect.top + nextRect.height / 2,
156
+ },
157
+ size: rect.height,
158
+ direction: 1,
159
+ })
160
+ } else {
161
+ insertAreas.push({
162
+ layout: 'inline',
163
+ parent,
164
+ index: index + 1,
165
+ center: {
166
+ x: rect.right,
167
+ y: rect.top + rect.height / 2,
168
+ },
169
+ size: rect.height,
170
+ direction: 1,
171
+ })
172
+ }
173
+ } else if (siblings.length > 0) {
174
+ insertAreas.push({
175
+ layout: 'inline',
176
+ parent,
177
+ index: index + 1,
178
+ center: {
179
+ x: rect.right,
180
+ y: rect.top + rect.height / 2,
181
+ },
182
+ size: rect.height,
183
+ direction: 1,
184
+ })
185
+ }
186
+ }
187
+ })
188
+
189
+ return offsetDropLines(insertAreas)
190
+ }
191
+
192
+ /**
193
+ * As a post-effect, lines are moved towards their element if there are multiple overlapping lines.
194
+ * Pointing the cursor slightly outside a container will drop the element after the container, and
195
+ * dropping inside will add as the last child.
196
+ */
197
+ function offsetDropLines(insertAreas: Array<InsertArea>) {
198
+ return insertAreas.map((area) => {
199
+ if (area.layout === 'block') {
200
+ return {
201
+ ...area,
202
+ point: {
203
+ ...area.center,
204
+ y:
205
+ area.center.y -
206
+ insertAreas.filter(
207
+ (area2) =>
208
+ area.parent !== area2.parent &&
209
+ area2.layout === 'block' &&
210
+ area2.center.y === area.center.y &&
211
+ area2.parent.contains(area.parent),
212
+ ).length *
213
+ area.direction,
214
+ },
215
+ }
216
+ } else {
217
+ return {
218
+ ...area,
219
+ point: {
220
+ ...area.center,
221
+ x:
222
+ area.center.x -
223
+ insertAreas.filter(
224
+ (area2) =>
225
+ area.parent !== area2.parent &&
226
+ area2.layout === 'inline' &&
227
+ area2.center.x === area.center.x &&
228
+ area2.parent.contains(area.parent),
229
+ ).length *
230
+ area.direction,
231
+ },
232
+ }
233
+ }
234
+ })
235
+ }
@@ -0,0 +1,36 @@
1
+ export type DragState = {
2
+ /**
3
+ * Dragging elements within the initial container is a reorder operation while dragging elements outside the initial container is an insert operation.
4
+ * While they share some common properties, we need to differentiate between the two to handle them differently.
5
+ */
6
+ mode: 'reorder' | 'insert'
7
+ elementType: 'element' | 'component' | 'text'
8
+ copy?: HTMLElement
9
+ element: HTMLElement
10
+ repeatedNodes: HTMLElement[]
11
+ offset: Point
12
+ lastCursorPosition: Point
13
+ initialContainer: HTMLElement
14
+ initialNextSibling: Element | null
15
+ initialRect: DOMRect
16
+ reorderPermutations: Array<{
17
+ nextSibling: Node | null
18
+ rect: DOMRect
19
+ }>
20
+ isTransitioning: boolean
21
+ selectedInsertAreaIndex?: number
22
+ insertAreas?: Array<InsertArea>
23
+ destroying: boolean
24
+ }
25
+
26
+ export type InsertArea = {
27
+ layout: 'block' | 'inline'
28
+ parent: Element
29
+ index: number
30
+ center: Point
31
+ size: number
32
+ direction: 1 | -1
33
+ }
34
+
35
+ export type Point = { x: number; y: number }
36
+ export type Line = { x1: number; y1: number; x2: number; y2: number }