-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.js
More file actions
135 lines (128 loc) · 4.23 KB
/
model.js
File metadata and controls
135 lines (128 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Data model factories — HyperCard PWA
*/
let _seq = Date.now();
export function generateId(prefix = 'obj') {
return `${prefix}-${(++_seq).toString(36)}`;
}
export function createStack(p = {}) {
return {
id: p.id || generateId('stack'),
name: p.name || 'Untitled Stack',
script: p.script || '',
cardSize: p.cardSize || { width: 1200, height: 800 },
cardIds: p.cardIds || [],
backgroundIds: p.backgroundIds || [],
createdAt: p.createdAt || Date.now(),
backdropColor: p.backdropColor || '#e0e0e0',
backgroundColor: p.backgroundColor || '#ffffff',
pattern: p.pattern || 'none',
patternColor: p.patternColor || '#d0d0d0',
style: p.style || 'rectangle',
isTransparent: p.isTransparent || false,
backgroundImage: p.backgroundImage || null,
};
}
export function createBackground(p = {}) {
return {
id: p.id || generateId('bg'),
stackId: p.stackId || '',
name: p.name || 'Background 1',
script: p.script || '',
backgroundColor: p.backgroundColor || '#ffffff',
backgroundImage: p.backgroundImage || null,
objects: p.objects || [],
isTransparent: p.isTransparent || false,
};
}
export function createCard(p = {}) {
return {
id: p.id || generateId('card'),
stackId: p.stackId || '',
backgroundId: p.backgroundId || '',
name: p.name || 'Untitled Card',
number: p.number || 1,
script: p.script || '',
backgroundColor: p.backgroundColor || null,
backgroundImage: p.backgroundImage || null,
objects: p.objects || [],
isTransparent: p.isTransparent || false,
};
}
export function createObject(p = {}) {
const type = p.type || 'button';
const defW = type === 'embed' ? 560 : type === 'emoji' ? 80 : type === 'button' ? 130 : 200;
const defH = type === 'embed' ? 315 : type === 'emoji' ? 80 : type === 'button' ? 36 : 100;
const base = {
id: p.id || generateId(type),
type,
name: p.name || _defaultName(type),
rect: p.rect || { x: 80, y: 80, width: defW, height: defH },
visible: p.visible !== false,
hideOnHome: p.hideOnHome || false,
script: p.script || '',
layer: p.layer || 'card',
rotation: p.rotation || 0,
};
if (type === 'button') {
return {
...base,
title: p.title !== undefined ? p.title : base.name,
style: p.style || 'roundrect',
textColor: p.textColor || '#000000',
fillColor: p.fillColor || '#dddddd',
fontSize: p.fontSize || 16,
fontFamily: p.fontFamily || 'system-ui,sans-serif',
enabled: p.enabled !== false,
autoHilite: p.autoHilite !== false,
};
}
if (type === 'field') {
return {
...base,
content: p.content || '',
style: p.style || 'rectangle',
textColor: p.textColor || '#000000',
fillColor: p.fillColor || '#ffffff',
fontSize: p.fontSize || 16,
fontFamily: p.fontFamily || 'system-ui,sans-serif',
editable: p.editable !== false,
lockText: p.lockText || false,
multiLine: p.multiLine !== false,
allowLinks: p.allowLinks !== false,
};
}
if (type === 'image') {
return {
...base,
src: p.src || '',
alt: p.alt || '',
fit: p.fit || 'cover',
};
}
if (type === 'audio') {
return {
...base,
src: p.src || '',
controls: true,
autoplay: false,
loop: false,
};
}
if (type === 'embed') {
return {
...base,
htmlCode: p.htmlCode || '<iframe src="https://example.com" width="100%" height="100%" frameborder="0"></iframe>',
};
}
if (type === 'emoji') {
return {
...base,
emoji: p.emoji || '🚀',
};
}
return base;
}
function _defaultName(type) {
return { button: 'New Button', field: 'New Field', image: 'New Image', audio: 'New Audio', embed: 'New Embed', emoji: 'New Emoji' }[type] || 'Object';
}