@craftile/core 0.2.1 → 0.3.1
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@craftile/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Framework-agnostic block editor engine for Craftile",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"craftile",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"src"
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@craftile/event-bus": "0.
|
|
30
|
-
"@craftile/types": "0.
|
|
29
|
+
"@craftile/event-bus": "0.3.1",
|
|
30
|
+
"@craftile/types": "0.3.1"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"vitest": "^3.2.4",
|
|
@@ -63,6 +63,7 @@ export class InsertBlockFromPresetCommand implements Command {
|
|
|
63
63
|
this.insertedBlock = {
|
|
64
64
|
type: this.blockType,
|
|
65
65
|
id: this.blockId,
|
|
66
|
+
name: preset.name,
|
|
66
67
|
properties: this.properties,
|
|
67
68
|
children: [],
|
|
68
69
|
parentId: undefined,
|
|
@@ -176,6 +177,7 @@ export class InsertBlockFromPresetCommand implements Command {
|
|
|
176
177
|
const childBlock: Block = {
|
|
177
178
|
type: presetChild.type,
|
|
178
179
|
id: childId,
|
|
180
|
+
name: childSchema.meta?.name || presetChild.type, // Use schema name for child blocks
|
|
179
181
|
semanticId: presetChild.id, // Preserve semantic ID from preset
|
|
180
182
|
properties: childProperties,
|
|
181
183
|
children: [],
|
|
@@ -20,6 +20,7 @@ export class InsertBlockCommand implements Command {
|
|
|
20
20
|
private index?: number;
|
|
21
21
|
private blockId: string;
|
|
22
22
|
private properties: Record<string, any>;
|
|
23
|
+
private blockSchema?: BlockSchema;
|
|
23
24
|
private insertedBlock?: Block;
|
|
24
25
|
private actualIndex?: number;
|
|
25
26
|
private emit: EngineEmitFn;
|
|
@@ -32,14 +33,18 @@ export class InsertBlockCommand implements Command {
|
|
|
32
33
|
this.index = options.index;
|
|
33
34
|
this.blockId = generateId();
|
|
34
35
|
this.emit = options.emit;
|
|
36
|
+
this.blockSchema = options.blockSchema;
|
|
35
37
|
|
|
36
38
|
this.properties = this.buildProperties(options.blockSchema);
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
apply(): void {
|
|
42
|
+
const blockName = this.blockSchema?.meta?.name || this.blockType;
|
|
43
|
+
|
|
40
44
|
this.insertedBlock = {
|
|
41
45
|
type: this.blockType,
|
|
42
46
|
id: this.blockId,
|
|
47
|
+
name: blockName,
|
|
43
48
|
properties: this.properties,
|
|
44
49
|
children: [],
|
|
45
50
|
parentId: undefined, // Will be set if inserting as child
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Block, Page } from '@craftile/types';
|
|
2
|
+
import type { Command, EngineEmitFn } from '../types';
|
|
3
|
+
|
|
4
|
+
export interface SetBlockNameOptions {
|
|
5
|
+
blockId: string;
|
|
6
|
+
name: string;
|
|
7
|
+
emit: EngineEmitFn;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class SetBlockNameCommand implements Command {
|
|
11
|
+
private page: Page;
|
|
12
|
+
private blockId: string;
|
|
13
|
+
private newName: string;
|
|
14
|
+
private oldName: string | undefined;
|
|
15
|
+
private block: Block | undefined;
|
|
16
|
+
private emit: EngineEmitFn;
|
|
17
|
+
|
|
18
|
+
constructor(page: Page, options: SetBlockNameOptions) {
|
|
19
|
+
this.page = page;
|
|
20
|
+
this.blockId = options.blockId;
|
|
21
|
+
this.newName = options.name;
|
|
22
|
+
this.emit = options.emit;
|
|
23
|
+
this.block = this.page.blocks[this.blockId];
|
|
24
|
+
|
|
25
|
+
if (!this.block) {
|
|
26
|
+
throw new Error(`Block not found: ${this.blockId}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
this.oldName = this.block.name;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
apply(): void {
|
|
33
|
+
if (!this.block) {
|
|
34
|
+
throw new Error(`Block not found: ${this.blockId}`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
this.block.name = this.newName;
|
|
38
|
+
|
|
39
|
+
this.emit('block:update', {
|
|
40
|
+
blockId: this.blockId,
|
|
41
|
+
block: this.block,
|
|
42
|
+
property: 'name',
|
|
43
|
+
value: this.newName,
|
|
44
|
+
oldValue: this.oldName,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
revert(): void {
|
|
49
|
+
if (!this.block) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
this.block.name = this.oldName;
|
|
54
|
+
|
|
55
|
+
this.emit('block:update', {
|
|
56
|
+
blockId: this.blockId,
|
|
57
|
+
block: this.block,
|
|
58
|
+
property: 'name',
|
|
59
|
+
value: this.oldName,
|
|
60
|
+
oldValue: this.newName,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/engine.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { MoveBlockCommand } from './commands/move-block';
|
|
|
10
10
|
import { ToggleBlockCommand } from './commands/toggle-block';
|
|
11
11
|
import { SetBlockPropertyCommand } from './commands/set-block-property';
|
|
12
12
|
import { DuplicateBlockCommand } from './commands/duplicate-block';
|
|
13
|
+
import { SetBlockNameCommand } from './commands/set-block-name';
|
|
13
14
|
|
|
14
15
|
export class Engine extends EventBus<EngineEvents> {
|
|
15
16
|
protected page!: Page;
|
|
@@ -233,6 +234,25 @@ export class Engine extends EventBus<EngineEvents> {
|
|
|
233
234
|
this.historyManager.addCommand(command);
|
|
234
235
|
}
|
|
235
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Set the display name for a block
|
|
239
|
+
*
|
|
240
|
+
* @param blockId - ID of the target block
|
|
241
|
+
* @param name - The new display name for the block
|
|
242
|
+
* @throws {Error} When block is not found
|
|
243
|
+
* @emits block:update - When the name is successfully set
|
|
244
|
+
*/
|
|
245
|
+
setBlockName(blockId: string, name: string): void {
|
|
246
|
+
const command = new SetBlockNameCommand(this.page, {
|
|
247
|
+
blockId,
|
|
248
|
+
name,
|
|
249
|
+
emit: this.emit.bind(this),
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
command.apply();
|
|
253
|
+
this.historyManager.addCommand(command);
|
|
254
|
+
}
|
|
255
|
+
|
|
236
256
|
/**
|
|
237
257
|
* Duplicate a block (creates a copy placed right after the original)
|
|
238
258
|
*/
|
package/src/types.ts
CHANGED
|
@@ -19,6 +19,7 @@ export interface EngineEvents {
|
|
|
19
19
|
'block:remove': { blockId: string; block: Block; parentId?: string };
|
|
20
20
|
'block:toggle': { blockId: string; disabled: boolean; oldValue?: boolean };
|
|
21
21
|
'block:property:set': { blockId: string; key: string; value: any; oldValue: any };
|
|
22
|
+
'block:update': { blockId: string; block: Block; property: string; value: any; oldValue: any };
|
|
22
23
|
'block:move': {
|
|
23
24
|
blockId: string;
|
|
24
25
|
targetParentId?: string;
|
|
@@ -51,6 +51,7 @@ export interface UseBlocksEngineReturn {
|
|
|
51
51
|
}
|
|
52
52
|
) => void;
|
|
53
53
|
setBlockProperty: (blockId: string, propertyKey: string, propertyValue: any) => void;
|
|
54
|
+
setBlockName: (blockId: string, name: string) => void;
|
|
54
55
|
toggleBlock: (blockId: string, disabled?: boolean) => void;
|
|
55
56
|
duplicateBlock: (blockId: string) => string;
|
|
56
57
|
|
|
@@ -168,6 +169,7 @@ export function useBlocksEngine(
|
|
|
168
169
|
'block:toggle',
|
|
169
170
|
'block:duplicate',
|
|
170
171
|
'block:property:set',
|
|
172
|
+
'block:update',
|
|
171
173
|
'undo',
|
|
172
174
|
'redo',
|
|
173
175
|
] as const;
|
|
@@ -247,6 +249,14 @@ export function useBlocksEngine(
|
|
|
247
249
|
}
|
|
248
250
|
};
|
|
249
251
|
|
|
252
|
+
const setBlockName = (blockId: string, name: string): void => {
|
|
253
|
+
engine.setBlockName(blockId, name);
|
|
254
|
+
|
|
255
|
+
if (!autoSync) {
|
|
256
|
+
syncStateFromEngine();
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
|
|
250
260
|
const toggleBlock = (blockId: string, disabled?: boolean): void => {
|
|
251
261
|
engine.toggleBlock(blockId, disabled);
|
|
252
262
|
|
|
@@ -319,6 +329,7 @@ export function useBlocksEngine(
|
|
|
319
329
|
removeBlock,
|
|
320
330
|
moveBlock,
|
|
321
331
|
setBlockProperty,
|
|
332
|
+
setBlockName,
|
|
322
333
|
toggleBlock,
|
|
323
334
|
duplicateBlock,
|
|
324
335
|
|