@nuforge/editor 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 nuforge authors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,693 @@
1
+ 'use strict';
2
+
3
+ var t = require('@nuforge/types');
4
+ var parser = require('@nuforge/parser');
5
+
6
+ function _interopNamespaceDefault(e) {
7
+ var n = Object.create(null);
8
+ if (e) {
9
+ Object.keys(e).forEach(function (k) {
10
+ if (k !== 'default') {
11
+ var d = Object.getOwnPropertyDescriptor(e, k);
12
+ Object.defineProperty(n, k, d.get ? d : {
13
+ enumerable: true,
14
+ get: function () { return e[k]; }
15
+ });
16
+ }
17
+ });
18
+ }
19
+ n.default = e;
20
+ return Object.freeze(n);
21
+ }
22
+
23
+ var t__namespace = /*#__PURE__*/_interopNamespaceDefault(t);
24
+
25
+ function lit(value) {
26
+ return t__namespace.literal({ value });
27
+ }
28
+ function text(value) {
29
+ return t__namespace.tagTemplate({
30
+ tag: 'text',
31
+ props: { text: lit(value) },
32
+ children: [],
33
+ });
34
+ }
35
+ function el(tag, props = {}, children = []) {
36
+ return t__namespace.tagTemplate({ tag, props, children });
37
+ }
38
+
39
+ function defineBlock(block) {
40
+ return block;
41
+ }
42
+ class BlockRegistry {
43
+ blocks = new Map();
44
+ constructor(initial = []) {
45
+ for (const block of initial) {
46
+ this.register(block);
47
+ }
48
+ }
49
+ register(block) {
50
+ this.blocks.set(block.id, block);
51
+ }
52
+ get(id) {
53
+ return this.blocks.get(id);
54
+ }
55
+ all() {
56
+ return [...this.blocks.values()];
57
+ }
58
+ byCategory() {
59
+ const out = {};
60
+ for (const block of this.blocks.values()) {
61
+ const key = block.category ?? 'Elements';
62
+ (out[key] ??= []).push(block);
63
+ }
64
+ return out;
65
+ }
66
+ }
67
+ const DEFAULT_BLOCKS = [
68
+ { id: 'box', label: 'Box', icon: 'square', create: () => el('div') },
69
+ { id: 'text', label: 'Text', icon: 'type', create: () => text('Text') },
70
+ {
71
+ id: 'heading',
72
+ label: 'Heading',
73
+ icon: 'heading',
74
+ create: () => el('h2', {}, [text('Heading')]),
75
+ },
76
+ {
77
+ id: 'paragraph',
78
+ label: 'Paragraph',
79
+ icon: 'pilcrow',
80
+ create: () => el('p', {}, [text('Paragraph text')]),
81
+ },
82
+ {
83
+ id: 'button',
84
+ label: 'Button',
85
+ icon: 'mouse-pointer-click',
86
+ create: () => el('button', {}, [text('Button')]),
87
+ },
88
+ {
89
+ id: 'container',
90
+ label: 'Container',
91
+ icon: 'box',
92
+ create: () => el('section', {}, [el('h3', {}, [text('Section')])]),
93
+ },
94
+ ];
95
+
96
+ class CommandRegistry {
97
+ editor;
98
+ commands = new Map();
99
+ constructor(editor) {
100
+ this.editor = editor;
101
+ }
102
+ register(command) {
103
+ this.commands.set(command.id, command);
104
+ }
105
+ get(id) {
106
+ return this.commands.get(id);
107
+ }
108
+ all() {
109
+ return [...this.commands.values()];
110
+ }
111
+ run(id) {
112
+ this.commands.get(id)?.run(this.editor);
113
+ }
114
+ }
115
+
116
+ function dropPositionFromPointer(clientY, rect, opts = {}) {
117
+ const ratio = rect.height > 0 ? (clientY - rect.top) / rect.height : 0;
118
+ if (opts.allowInside) {
119
+ if (ratio < 0.25)
120
+ return 'before';
121
+ if (ratio > 0.75)
122
+ return 'after';
123
+ return 'inside';
124
+ }
125
+ return ratio < 0.5 ? 'before' : 'after';
126
+ }
127
+ function templateLabel(node) {
128
+ if (node instanceof t__namespace.TagTemplate) {
129
+ return node.tag;
130
+ }
131
+ if (node instanceof t__namespace.ComponentTemplate) {
132
+ return node.component.name;
133
+ }
134
+ if (node instanceof t__namespace.SlotTemplate) {
135
+ return node.name ? `slot: ${node.name}` : 'slot';
136
+ }
137
+ if (t__namespace.is(node, t__namespace.FragmentTemplate)) {
138
+ return 'fragment';
139
+ }
140
+ return 'node';
141
+ }
142
+ function templateChildren(node) {
143
+ return (node.children ?? []).filter((child) => child instanceof t__namespace.Type);
144
+ }
145
+ function isTextTemplate(node) {
146
+ return node instanceof t__namespace.TagTemplate && node.tag === 'text';
147
+ }
148
+ function textPreview(node) {
149
+ if (!isTextTemplate(node)) {
150
+ return null;
151
+ }
152
+ const expr = node.props.text;
153
+ if (expr instanceof t__namespace.Literal) {
154
+ return String(expr.value);
155
+ }
156
+ return '{…}';
157
+ }
158
+ function canHaveChildren(node) {
159
+ if (!node || isTextTemplate(node)) {
160
+ return false;
161
+ }
162
+ return (node instanceof t__namespace.TagTemplate ||
163
+ node instanceof t__namespace.ComponentTemplate ||
164
+ t__namespace.is(node, t__namespace.FragmentTemplate));
165
+ }
166
+ function isAncestorOf(nu, ancestor, node) {
167
+ let parent = nu.getParentNode(node);
168
+ while (parent) {
169
+ if (parent === ancestor) {
170
+ return true;
171
+ }
172
+ parent = nu.getParentNode(parent);
173
+ }
174
+ return false;
175
+ }
176
+
177
+ class Editor {
178
+ nu;
179
+ blocks;
180
+ commands;
181
+ constructor(nu, opts = {}) {
182
+ this.nu = nu;
183
+ const base = opts.defaultBlocks === false ? [] : DEFAULT_BLOCKS;
184
+ this.blocks = new BlockRegistry([...base, ...(opts.blocks ?? [])]);
185
+ this.commands = new CommandRegistry(this);
186
+ this.commands.register({
187
+ id: 'undo',
188
+ label: 'Undo',
189
+ shortcut: 'mod+z',
190
+ run: (e) => e.nu.undo(),
191
+ });
192
+ this.commands.register({
193
+ id: 'redo',
194
+ label: 'Redo',
195
+ shortcut: 'mod+shift+z',
196
+ run: (e) => e.nu.redo(),
197
+ });
198
+ }
199
+ insertNode(parent, child, index) {
200
+ const kids = parent.children;
201
+ if (!Array.isArray(kids)) {
202
+ return;
203
+ }
204
+ this.nu.change(() => {
205
+ if (index === undefined || index >= kids.length) {
206
+ kids.push(child);
207
+ }
208
+ else {
209
+ kids.splice(Math.max(0, index), 0, child);
210
+ }
211
+ });
212
+ }
213
+ removeNode(node) {
214
+ let ok = false;
215
+ this.nu.change(() => {
216
+ ok = this.spliceFromParent(node);
217
+ });
218
+ return ok;
219
+ }
220
+ moveNode(dragged, target, position) {
221
+ if (dragged === target || isAncestorOf(this.nu, dragged, target)) {
222
+ return false;
223
+ }
224
+ let ok = false;
225
+ this.nu.change(() => {
226
+ ok = this.applyMove(dragged, target, position);
227
+ });
228
+ return ok;
229
+ }
230
+ clipboardNode = null;
231
+ copyNode(node) {
232
+ this.clipboardNode = node;
233
+ }
234
+ cutNode(node) {
235
+ this.copyNode(node);
236
+ return this.removeNode(node);
237
+ }
238
+ canPaste() {
239
+ return this.clipboardNode !== null;
240
+ }
241
+ pasteNode(target, position = 'after') {
242
+ if (!this.clipboardNode) {
243
+ return null;
244
+ }
245
+ return this.insertCloneRelativeTo(this.clipboardNode, target, position);
246
+ }
247
+ duplicateNode(node) {
248
+ return this.insertCloneRelativeTo(node, node, 'after');
249
+ }
250
+ insertNodeAt(node, target, position) {
251
+ let ok = false;
252
+ this.nu.change(() => {
253
+ ok = this.insertRelativeTo(node, target, position);
254
+ });
255
+ return ok ? (this.nu.getNodeFromId(node.id) ?? null) : null;
256
+ }
257
+ insertBlockAt(blockId, target, position) {
258
+ const block = this.blocks.get(blockId);
259
+ if (!block) {
260
+ return null;
261
+ }
262
+ return this.insertNodeAt(block.create(), target, position);
263
+ }
264
+ insertCloneRelativeTo(source, target, position) {
265
+ const clone = t__namespace.clone(source, { replaceExistingId: true });
266
+ let ok = false;
267
+ this.nu.change(() => {
268
+ ok = this.insertRelativeTo(clone, target, position);
269
+ });
270
+ return ok ? (this.nu.getNodeFromId(clone.id) ?? null) : null;
271
+ }
272
+ updateProps(node, props) {
273
+ this.nu.change(() => {
274
+ for (const [key, value] of Object.entries(props)) {
275
+ node.props[key] = value;
276
+ }
277
+ });
278
+ }
279
+ setProp(node, key, expr) {
280
+ this.nu.change(() => {
281
+ node.props[key] = expr;
282
+ });
283
+ }
284
+ removeProp(node, key) {
285
+ this.nu.change(() => {
286
+ delete node.props[key];
287
+ });
288
+ }
289
+ setExpression(node, key, source) {
290
+ let expr;
291
+ try {
292
+ expr = parser.Parser.parseExpression(source);
293
+ }
294
+ catch {
295
+ return false;
296
+ }
297
+ this.setProp(node, key, expr);
298
+ return true;
299
+ }
300
+ setText(node, value) {
301
+ const expr = node.props?.text;
302
+ if (!(expr instanceof t__namespace.Literal)) {
303
+ return false;
304
+ }
305
+ this.nu.change(() => {
306
+ expr.value = value;
307
+ });
308
+ return true;
309
+ }
310
+ setTag(node, tag) {
311
+ const clean = tag.trim();
312
+ if (!clean) {
313
+ return;
314
+ }
315
+ this.nu.change(() => {
316
+ node.tag = clean;
317
+ });
318
+ }
319
+ addClass(node, name) {
320
+ const clean = name.trim();
321
+ if (!clean) {
322
+ return;
323
+ }
324
+ this.nu.change(() => {
325
+ const tag = node;
326
+ if (!tag.classList) {
327
+ tag.classList = t__namespace.objectExpression({ properties: {} });
328
+ }
329
+ tag.classList.properties[clean] = t__namespace.literal({ value: true });
330
+ });
331
+ }
332
+ setClassCondition(node, name, source) {
333
+ let expr;
334
+ try {
335
+ expr = parser.Parser.parseExpression(source);
336
+ }
337
+ catch {
338
+ return false;
339
+ }
340
+ this.nu.change(() => {
341
+ const tag = node;
342
+ if (tag.classList) {
343
+ tag.classList.properties[name] = expr;
344
+ }
345
+ });
346
+ return true;
347
+ }
348
+ removeClass(node, name) {
349
+ this.nu.change(() => {
350
+ const tag = node;
351
+ if (!tag.classList) {
352
+ return;
353
+ }
354
+ delete tag.classList.properties[name];
355
+ if (Object.keys(tag.classList.properties).length === 0) {
356
+ tag.classList = null;
357
+ }
358
+ });
359
+ }
360
+ setCondition(node, source) {
361
+ let expr;
362
+ try {
363
+ expr = parser.Parser.parseExpression(source);
364
+ }
365
+ catch {
366
+ return false;
367
+ }
368
+ this.nu.change(() => {
369
+ node.if = expr;
370
+ });
371
+ return true;
372
+ }
373
+ clearCondition(node) {
374
+ this.nu.change(() => {
375
+ node.if = null;
376
+ });
377
+ }
378
+ setEach(node, iterator, alias = 'item', indexName) {
379
+ let expr;
380
+ try {
381
+ expr = parser.Parser.parseExpression(iterator);
382
+ }
383
+ catch {
384
+ return false;
385
+ }
386
+ this.nu.change(() => {
387
+ node.each = t__namespace.elementEach({
388
+ alias: t__namespace.elementEachAlias({ name: alias || 'item' }),
389
+ index: indexName ? t__namespace.elementEachIndex({ name: indexName }) : null,
390
+ iterator: expr,
391
+ });
392
+ });
393
+ return true;
394
+ }
395
+ clearEach(node) {
396
+ this.nu.change(() => {
397
+ node.each = null;
398
+ });
399
+ }
400
+ addState(component, name = 'value', init = '0') {
401
+ const taken = new Set(component.state.map((v) => v.name));
402
+ let unique = name;
403
+ let i = 2;
404
+ while (taken.has(unique)) {
405
+ unique = `${name}${i}`;
406
+ i += 1;
407
+ }
408
+ let initExpr;
409
+ try {
410
+ initExpr = parser.Parser.parseExpression(init);
411
+ }
412
+ catch {
413
+ initExpr = t__namespace.literal({ value: 0 });
414
+ }
415
+ this.nu.change(() => {
416
+ component.state.push(t__namespace.val({ name: unique, init: initExpr }));
417
+ });
418
+ return component.state.find((v) => v.name === unique);
419
+ }
420
+ removeState(component, val) {
421
+ this.nu.change(() => {
422
+ const i = component.state.findIndex((v) => v.id === val.id);
423
+ if (i >= 0) {
424
+ component.state.splice(i, 1);
425
+ }
426
+ });
427
+ }
428
+ setStateInit(val, source) {
429
+ let expr;
430
+ try {
431
+ expr = parser.Parser.parseExpression(source);
432
+ }
433
+ catch {
434
+ return false;
435
+ }
436
+ this.nu.change(() => {
437
+ val.init = expr;
438
+ });
439
+ return true;
440
+ }
441
+ addComponentProp(component, name = 'prop', init) {
442
+ const taken = new Set(component.props.map((p) => p.name));
443
+ let unique = name;
444
+ let i = 2;
445
+ while (taken.has(unique)) {
446
+ unique = `${name}${i}`;
447
+ i += 1;
448
+ }
449
+ let initExpr;
450
+ if (init) {
451
+ try {
452
+ initExpr = parser.Parser.parseExpression(init);
453
+ }
454
+ catch {
455
+ initExpr = undefined;
456
+ }
457
+ }
458
+ this.nu.change(() => {
459
+ component.props.push(t__namespace.componentProp({ name: unique, init: initExpr }));
460
+ });
461
+ return component.props.find((p) => p.name === unique);
462
+ }
463
+ removeComponentProp(component, prop) {
464
+ this.nu.change(() => {
465
+ const i = component.props.findIndex((p) => p.id === prop.id);
466
+ if (i >= 0) {
467
+ component.props.splice(i, 1);
468
+ }
469
+ });
470
+ }
471
+ setComponentPropInit(prop, source) {
472
+ let expr;
473
+ try {
474
+ expr = parser.Parser.parseExpression(source);
475
+ }
476
+ catch {
477
+ return false;
478
+ }
479
+ this.nu.change(() => {
480
+ prop.init = expr;
481
+ });
482
+ return true;
483
+ }
484
+ insertBlock(blockId, parent, index) {
485
+ const block = this.blocks.get(blockId);
486
+ if (!block || !canHaveChildren(parent)) {
487
+ return null;
488
+ }
489
+ const created = block.create();
490
+ this.insertNode(parent, created, index);
491
+ return created;
492
+ }
493
+ uniqueName(base) {
494
+ const taken = new Set(this.getComponents().map((c) => c.name));
495
+ if (!taken.has(base)) {
496
+ return base;
497
+ }
498
+ let i = 2;
499
+ while (taken.has(`${base}${i}`)) {
500
+ i += 1;
501
+ }
502
+ return `${base}${i}`;
503
+ }
504
+ addComponent(name = 'Page') {
505
+ const unique = this.uniqueName(name);
506
+ const component = t__namespace.nuComponent({
507
+ name: unique,
508
+ props: [],
509
+ state: [],
510
+ template: el('div', {}, [text(unique)]),
511
+ });
512
+ this.nu.change(() => {
513
+ this.nu.state.program.components.push(component);
514
+ });
515
+ return this.getComponent(unique);
516
+ }
517
+ renameComponent(component, name) {
518
+ const clean = name.trim();
519
+ if (!clean ||
520
+ this.getComponents().some((c) => c !== component && c.name === clean)) {
521
+ return false;
522
+ }
523
+ const old = component.name;
524
+ this.nu.change(() => {
525
+ component.name = clean;
526
+ for (const c of this.getComponents()) {
527
+ this.walkTemplates(c.template, (tpl) => {
528
+ if (tpl instanceof t__namespace.ComponentTemplate && tpl.component.name === old) {
529
+ tpl.component.name = clean;
530
+ }
531
+ });
532
+ }
533
+ });
534
+ return true;
535
+ }
536
+ removeComponent(component) {
537
+ const comps = this.nu.state.program.components;
538
+ let ok = false;
539
+ this.nu.change(() => {
540
+ const i = comps.findIndex((c) => c.id === component.id);
541
+ if (i >= 0) {
542
+ comps.splice(i, 1);
543
+ ok = true;
544
+ }
545
+ });
546
+ return ok;
547
+ }
548
+ duplicateComponent(component) {
549
+ const copy = t__namespace.clone(component, { replaceExistingId: true });
550
+ const name = this.uniqueName(`${component.name}Copy`);
551
+ copy.name = name;
552
+ this.nu.change(() => {
553
+ this.nu.state.program.components.push(copy);
554
+ });
555
+ return this.getComponent(name);
556
+ }
557
+ walkTemplates(node, fn) {
558
+ if (!node) {
559
+ return;
560
+ }
561
+ fn(node);
562
+ for (const child of templateChildren(node)) {
563
+ this.walkTemplates(child, fn);
564
+ }
565
+ }
566
+ getComponents() {
567
+ return this.nu.state.program.components.filter((c) => c instanceof t__namespace.NuComponent);
568
+ }
569
+ getComponent(name) {
570
+ return this.getComponents().find((c) => c.name === name);
571
+ }
572
+ getRootTemplate(name) {
573
+ return this.getComponent(name)?.template ?? null;
574
+ }
575
+ getChildren(node) {
576
+ return templateChildren(node);
577
+ }
578
+ getSiblings(node) {
579
+ const parent = this.nu.getParentNode(node);
580
+ return parent ? templateChildren(parent) : [];
581
+ }
582
+ getNodeIndex(node) {
583
+ return this.getSiblings(node).indexOf(node);
584
+ }
585
+ getNodePath(node) {
586
+ const out = [];
587
+ let current = node;
588
+ let loc = this.nu.getNodeLocation(current);
589
+ while (loc && loc.parent !== current) {
590
+ out.unshift(...loc.path);
591
+ current = loc.parent;
592
+ loc = this.nu.getNodeLocation(current);
593
+ }
594
+ return out;
595
+ }
596
+ onChange(cb) {
597
+ return this.nu.listenToMutations(cb);
598
+ }
599
+ onStructureChange(cb) {
600
+ return this.nu.listenToMutations((entries) => {
601
+ if (entries.some((e) => Array.isArray(e.target))) {
602
+ cb();
603
+ }
604
+ });
605
+ }
606
+ spliceFromParent(node) {
607
+ const parent = this.nu.getParentNode(node);
608
+ const kids = parent?.children;
609
+ if (!Array.isArray(kids)) {
610
+ return false;
611
+ }
612
+ const index = kids.indexOf(node);
613
+ if (index < 0) {
614
+ return false;
615
+ }
616
+ kids.splice(index, 1);
617
+ return true;
618
+ }
619
+ applyMove(dragged, target, position) {
620
+ const oldParent = this.nu.getParentNode(dragged);
621
+ const oldKids = oldParent?.children;
622
+ if (!Array.isArray(oldKids)) {
623
+ return false;
624
+ }
625
+ if (position === 'inside') {
626
+ if (!canHaveChildren(target)) {
627
+ return false;
628
+ }
629
+ const destKids = target.children;
630
+ const oi = oldKids.indexOf(dragged);
631
+ if (oi >= 0) {
632
+ oldKids.splice(oi, 1);
633
+ }
634
+ destKids.push(dragged);
635
+ return true;
636
+ }
637
+ const newParent = this.nu.getParentNode(target);
638
+ const destKids = newParent?.children;
639
+ if (!Array.isArray(destKids)) {
640
+ return false;
641
+ }
642
+ const oi = oldKids.indexOf(dragged);
643
+ if (oi >= 0) {
644
+ oldKids.splice(oi, 1);
645
+ }
646
+ let ti = destKids.indexOf(target);
647
+ if (ti < 0) {
648
+ ti = destKids.length - 1;
649
+ }
650
+ destKids.splice(position === 'after' ? ti + 1 : ti, 0, dragged);
651
+ return true;
652
+ }
653
+ insertRelativeTo(node, target, position) {
654
+ if (position === 'inside') {
655
+ if (!canHaveChildren(target)) {
656
+ return false;
657
+ }
658
+ target.children.push(node);
659
+ return true;
660
+ }
661
+ const parent = this.nu.getParentNode(target);
662
+ const kids = parent?.children;
663
+ if (!Array.isArray(kids)) {
664
+ return false;
665
+ }
666
+ let ti = kids.indexOf(target);
667
+ if (ti < 0) {
668
+ ti = kids.length - 1;
669
+ }
670
+ kids.splice(position === 'after' ? ti + 1 : ti, 0, node);
671
+ return true;
672
+ }
673
+ }
674
+ function createEditor(nu, opts) {
675
+ return new Editor(nu, opts);
676
+ }
677
+
678
+ exports.BlockRegistry = BlockRegistry;
679
+ exports.CommandRegistry = CommandRegistry;
680
+ exports.DEFAULT_BLOCKS = DEFAULT_BLOCKS;
681
+ exports.Editor = Editor;
682
+ exports.canHaveChildren = canHaveChildren;
683
+ exports.createEditor = createEditor;
684
+ exports.defineBlock = defineBlock;
685
+ exports.dropPositionFromPointer = dropPositionFromPointer;
686
+ exports.el = el;
687
+ exports.isAncestorOf = isAncestorOf;
688
+ exports.isTextTemplate = isTextTemplate;
689
+ exports.lit = lit;
690
+ exports.templateChildren = templateChildren;
691
+ exports.templateLabel = templateLabel;
692
+ exports.text = text;
693
+ exports.textPreview = textPreview;