modern-idoc 0.4.5 → 0.5.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/dist/index.cjs CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const colord = require('colord');
4
+
3
5
  function normalizeAudio(audio) {
4
6
  if (!audio || audio === "none") {
5
7
  return void 0;
@@ -10,47 +12,147 @@ function normalizeAudio(audio) {
10
12
  }
11
13
  }
12
14
 
15
+ function parseColor(color) {
16
+ let input;
17
+ if (typeof color === "number") {
18
+ input = {
19
+ r: color >> 24 & 255,
20
+ g: color >> 16 & 255,
21
+ b: color >> 8 & 255,
22
+ a: (color & 255) / 255
23
+ };
24
+ } else {
25
+ input = color;
26
+ }
27
+ return colord.colord(input);
28
+ }
29
+ function normalizeColor(color, orFail = false) {
30
+ if (color === void 0 || color === "none") {
31
+ return void 0;
32
+ }
33
+ const parsed = parseColor(color);
34
+ if (!parsed.isValid()) {
35
+ const message = `Failed to normalizeColor ${color}`;
36
+ if (orFail) {
37
+ throw new Error(message);
38
+ } else {
39
+ console.warn(message);
40
+ return `rgb(0, 0, 0)`;
41
+ }
42
+ }
43
+ return parsed.toRgbString();
44
+ }
45
+
46
+ function normalizeFill(fill) {
47
+ if (!fill || fill === "none") {
48
+ return void 0;
49
+ } else if (typeof fill === "string") {
50
+ return { color: normalizeColor(fill) };
51
+ } else {
52
+ return {
53
+ ...fill,
54
+ color: normalizeColor(fill.color)
55
+ };
56
+ }
57
+ }
58
+
13
59
  function normalizeBackground(background) {
14
60
  if (!background || background === "none") {
15
61
  return void 0;
16
62
  } else if (typeof background === "string") {
17
63
  return { src: background };
18
64
  } else {
19
- return background;
65
+ return {
66
+ ...background,
67
+ ...normalizeFill(background)
68
+ };
20
69
  }
21
70
  }
22
71
 
23
- function normalizeEffect(effect) {
24
- if (!effect || effect === "none") {
25
- return void 0;
26
- } else {
27
- return effect;
72
+ function clearUndef(obj, deep = false) {
73
+ if (typeof obj !== "object" || !obj) {
74
+ return obj;
75
+ }
76
+ if (Array.isArray(obj)) {
77
+ if (deep) {
78
+ return obj.map((v) => clearUndef(v, deep));
79
+ } else {
80
+ return obj;
81
+ }
28
82
  }
83
+ const newObj = {};
84
+ for (const key in obj) {
85
+ const value = obj[key];
86
+ if (value === void 0 || value === null) {
87
+ continue;
88
+ }
89
+ if (deep) {
90
+ newObj[key] = clearUndef(value, deep);
91
+ } else {
92
+ newObj[key] = value;
93
+ }
94
+ }
95
+ return newObj;
29
96
  }
30
97
 
98
+ function getDefaultInnerShadow() {
99
+ return {
100
+ color: "rgb(0, 0, 0)",
101
+ offsetX: 0,
102
+ offsetY: 0,
103
+ blurRadius: 1
104
+ };
105
+ }
31
106
  function normalizeInnerShadow(shadow) {
32
107
  if (!shadow || shadow === "none") {
33
108
  return void 0;
34
109
  } else {
35
- return shadow;
110
+ return {
111
+ ...getDefaultInnerShadow(),
112
+ ...clearUndef({
113
+ ...shadow,
114
+ color: normalizeColor(shadow.color)
115
+ })
116
+ };
36
117
  }
37
118
  }
38
119
 
120
+ function getDefaultOuterShadow() {
121
+ return {
122
+ ...getDefaultInnerShadow(),
123
+ scaleX: 1,
124
+ scaleY: 1
125
+ };
126
+ }
39
127
  function normalizeOuterShadow(shadow) {
40
128
  if (!shadow || shadow === "none") {
41
129
  return void 0;
42
130
  } else {
43
- return shadow;
131
+ return {
132
+ ...getDefaultOuterShadow(),
133
+ ...normalizeInnerShadow(shadow)
134
+ };
44
135
  }
45
136
  }
46
137
 
47
- function normalizeFill(fill) {
48
- if (!fill || fill === "none") {
138
+ function normalizeSoftEdge(softEdge) {
139
+ if (!softEdge || softEdge === "none") {
49
140
  return void 0;
50
- } else if (typeof fill === "string") {
51
- return { color: fill };
52
141
  } else {
53
- return fill;
142
+ return softEdge;
143
+ }
144
+ }
145
+
146
+ function normalizeEffect(effect) {
147
+ if (!effect || effect === "none") {
148
+ return void 0;
149
+ } else {
150
+ return clearUndef({
151
+ ...effect,
152
+ softEdge: normalizeSoftEdge(effect.softEdge),
153
+ outerShadow: normalizeOuterShadow(effect.outerShadow),
154
+ innerShadow: normalizeInnerShadow(effect.innerShadow)
155
+ });
54
156
  }
55
157
  }
56
158
 
@@ -60,7 +162,10 @@ function normalizeForeground(foreground) {
60
162
  } else if (typeof foreground === "string") {
61
163
  return { src: foreground };
62
164
  } else {
63
- return foreground;
165
+ return {
166
+ ...foreground,
167
+ ...normalizeFill(foreground)
168
+ };
64
169
  }
65
170
  }
66
171
 
@@ -94,149 +199,34 @@ function normalizeOutline(outline) {
94
199
  return void 0;
95
200
  } else if (typeof outline === "string") {
96
201
  return {
97
- color: outline
202
+ color: normalizeColor(outline)
98
203
  };
99
204
  } else {
100
- return outline;
205
+ return {
206
+ ...outline,
207
+ color: normalizeColor(outline.color)
208
+ };
101
209
  }
102
210
  }
103
211
 
104
- function getDefaultShadowStyle() {
105
- return {
106
- boxShadow: "none"
107
- };
108
- }
109
212
  function normalizeShadow(shadow) {
110
213
  if (!shadow || shadow === "none") {
111
214
  return void 0;
112
215
  } else if (typeof shadow === "string") {
113
216
  return {
114
- color: shadow
115
- };
116
- } else {
117
- return shadow;
118
- }
119
- }
120
-
121
- function normalizeTextContent(content = "") {
122
- const list = Array.isArray(content) ? content : [content];
123
- return list.map((p) => {
124
- if (typeof p === "string") {
125
- return {
126
- fragments: [
127
- { content: p }
128
- ]
129
- };
130
- } else if ("content" in p) {
131
- return {
132
- fragments: [
133
- { ...p }
134
- ]
135
- };
136
- } else if ("fragments" in p) {
137
- return {
138
- ...p,
139
- fragments: p.fragments.map((f) => ({ ...f }))
140
- };
141
- } else if (Array.isArray(p)) {
142
- return {
143
- fragments: p.map((f) => {
144
- if (typeof f === "string") {
145
- return {
146
- content: f
147
- };
148
- } else {
149
- return { ...f };
150
- }
151
- })
152
- };
153
- } else {
154
- return {
155
- fragments: []
156
- };
157
- }
158
- });
159
- }
160
- function normalizeText(text) {
161
- if (!text || text === "none") {
162
- return void 0;
163
- } else if (typeof text === "string") {
164
- return {
165
- content: [
166
- {
167
- fragments: [
168
- { content: text }
169
- ]
170
- }
171
- ]
172
- };
173
- } else if ("content" in text) {
174
- return {
175
- ...text,
176
- content: normalizeTextContent(text.content)
217
+ color: normalizeColor(shadow)
177
218
  };
178
219
  } else {
179
220
  return {
180
- content: normalizeTextContent(text)
221
+ ...shadow,
222
+ color: normalizeColor(shadow.color)
181
223
  };
182
224
  }
183
225
  }
184
-
185
- function clearUndef(obj, deep = false) {
186
- if (typeof obj !== "object" || !obj) {
187
- return obj;
188
- }
189
- if (Array.isArray(obj)) {
190
- if (deep) {
191
- return obj.map((v) => clearUndef(v, deep));
192
- } else {
193
- return obj;
194
- }
195
- }
196
- const newObj = {};
197
- for (const key in obj) {
198
- const value = obj[key];
199
- if (value === void 0 || value === null) {
200
- continue;
201
- }
202
- if (deep) {
203
- newObj[key] = clearUndef(value, deep);
204
- } else {
205
- newObj[key] = value;
206
- }
207
- }
208
- return newObj;
209
- }
210
-
211
- function normalizeVideo(video) {
212
- if (!video || video === "none") {
213
- return void 0;
214
- } else if (typeof video === "string") {
215
- return { src: video };
216
- } else {
217
- return video;
218
- }
219
- }
220
-
221
- function normalizeElement(element) {
222
- return clearUndef({
223
- ...element,
224
- text: normalizeText(element.text),
225
- background: normalizeBackground(element.background),
226
- geometry: normalizeGeometry(element.geometry),
227
- fill: normalizeFill(element.fill),
228
- outline: normalizeOutline(element.outline),
229
- foreground: normalizeForeground(element.foreground),
230
- shadow: normalizeShadow(element.shadow),
231
- video: normalizeVideo(element.video),
232
- audio: normalizeVideo(element.audio),
233
- effect: normalizeEffect(element.effect),
234
- children: element.children?.map((child) => normalizeElement(child))
235
- });
236
- }
237
-
238
- function normalizeDocument(doc) {
239
- return normalizeElement(doc);
226
+ function getDefaultShadowStyle() {
227
+ return {
228
+ boxShadow: "none"
229
+ };
240
230
  }
241
231
 
242
232
  function getDefaultLayoutStyle() {
@@ -321,7 +311,7 @@ function getDefaultElementStyle() {
321
311
  // outline
322
312
  outlineWidth: 0,
323
313
  outlineOffset: 0,
324
- outlineColor: "#000000",
314
+ outlineColor: "rgb(0, 0, 0)",
325
315
  outlineStyle: "none",
326
316
  // other
327
317
  visibility: "visible",
@@ -334,6 +324,7 @@ function getDefaultElementStyle() {
334
324
 
335
325
  function getDefaultHighlightStyle() {
336
326
  return {
327
+ highlight: {},
337
328
  highlightImage: "none",
338
329
  highlightReferImage: "none",
339
330
  highlightColormap: "none",
@@ -345,6 +336,7 @@ function getDefaultHighlightStyle() {
345
336
 
346
337
  function getDefaultListStyleStyle() {
347
338
  return {
339
+ listStyle: {},
348
340
  listStyleType: "none",
349
341
  listStyleImage: "none",
350
342
  listStyleColormap: "none",
@@ -356,7 +348,7 @@ function getDefaultListStyleStyle() {
356
348
  function getDefaultTextInlineStyle() {
357
349
  return {
358
350
  ...getDefaultHighlightStyle(),
359
- color: "black",
351
+ color: "rgb(0, 0, 0)",
360
352
  verticalAlign: "baseline",
361
353
  letterSpacing: 0,
362
354
  wordSpacing: 0,
@@ -390,10 +382,23 @@ function getDefaultTextStyle() {
390
382
  ...getDefaultTextInlineStyle(),
391
383
  // textStroke
392
384
  textStrokeWidth: 0,
393
- textStrokeColor: "black"
385
+ textStrokeColor: "rgb(0, 0, 0)"
394
386
  };
395
387
  }
396
388
 
389
+ function normalizeStyle(style) {
390
+ if (!style || style === "none") {
391
+ return void 0;
392
+ }
393
+ return clearUndef({
394
+ ...style,
395
+ color: normalizeColor(style.color),
396
+ backgroundColor: normalizeColor(style.backgroundColor),
397
+ borderColor: normalizeColor(style.borderColor),
398
+ outlineColor: normalizeColor(style.outlineColor),
399
+ shadowColor: normalizeColor(style.shadowColor)
400
+ });
401
+ }
397
402
  function getDefaultStyle() {
398
403
  return {
399
404
  ...getDefaultElementStyle(),
@@ -401,11 +406,109 @@ function getDefaultStyle() {
401
406
  };
402
407
  }
403
408
 
409
+ function normalizeTextContent(content = "") {
410
+ const list = Array.isArray(content) ? content : [content];
411
+ return list.map((p) => {
412
+ if (typeof p === "string") {
413
+ return {
414
+ fragments: [
415
+ { content: p }
416
+ ]
417
+ };
418
+ } else if ("content" in p) {
419
+ return {
420
+ fragments: [
421
+ normalizeStyle(p)
422
+ ]
423
+ };
424
+ } else if ("fragments" in p) {
425
+ return {
426
+ ...normalizeStyle(p),
427
+ fragments: p.fragments.map((f) => normalizeStyle(f))
428
+ };
429
+ } else if (Array.isArray(p)) {
430
+ return {
431
+ fragments: p.map((f) => {
432
+ if (typeof f === "string") {
433
+ return {
434
+ content: f
435
+ };
436
+ } else {
437
+ return normalizeStyle(f);
438
+ }
439
+ })
440
+ };
441
+ } else {
442
+ return {
443
+ fragments: []
444
+ };
445
+ }
446
+ });
447
+ }
448
+ function normalizeText(text) {
449
+ if (!text || text === "none") {
450
+ return void 0;
451
+ } else if (typeof text === "string") {
452
+ return {
453
+ content: [
454
+ {
455
+ fragments: [
456
+ { content: text }
457
+ ]
458
+ }
459
+ ]
460
+ };
461
+ } else if ("content" in text) {
462
+ return {
463
+ ...text,
464
+ content: normalizeTextContent(text.content)
465
+ };
466
+ } else {
467
+ return {
468
+ content: normalizeTextContent(text)
469
+ };
470
+ }
471
+ }
472
+
473
+ function normalizeVideo(video) {
474
+ if (!video || video === "none") {
475
+ return void 0;
476
+ } else if (typeof video === "string") {
477
+ return { src: video };
478
+ } else {
479
+ return video;
480
+ }
481
+ }
482
+
483
+ function normalizeElement(element) {
484
+ return clearUndef({
485
+ ...element,
486
+ style: normalizeStyle(element.style),
487
+ text: normalizeText(element.text),
488
+ background: normalizeBackground(element.background),
489
+ geometry: normalizeGeometry(element.geometry),
490
+ fill: normalizeFill(element.fill),
491
+ outline: normalizeOutline(element.outline),
492
+ foreground: normalizeForeground(element.foreground),
493
+ shadow: normalizeShadow(element.shadow),
494
+ video: normalizeVideo(element.video),
495
+ audio: normalizeAudio(element.audio),
496
+ effect: normalizeEffect(element.effect),
497
+ children: element.children?.map((child) => normalizeElement(child))
498
+ });
499
+ }
500
+
501
+ function normalizeDocument(doc) {
502
+ return normalizeElement(doc);
503
+ }
504
+
404
505
  exports.clearUndef = clearUndef;
405
506
  exports.getDefaultElementStyle = getDefaultElementStyle;
406
507
  exports.getDefaultHighlightStyle = getDefaultHighlightStyle;
508
+ exports.getDefaultInnerShadow = getDefaultInnerShadow;
407
509
  exports.getDefaultLayoutStyle = getDefaultLayoutStyle;
408
510
  exports.getDefaultListStyleStyle = getDefaultListStyleStyle;
511
+ exports.getDefaultOuterShadow = getDefaultOuterShadow;
409
512
  exports.getDefaultShadowStyle = getDefaultShadowStyle;
410
513
  exports.getDefaultStyle = getDefaultStyle;
411
514
  exports.getDefaultTextInlineStyle = getDefaultTextInlineStyle;
@@ -414,6 +517,7 @@ exports.getDefaultTextStyle = getDefaultTextStyle;
414
517
  exports.getDefaultTransformStyle = getDefaultTransformStyle;
415
518
  exports.normalizeAudio = normalizeAudio;
416
519
  exports.normalizeBackground = normalizeBackground;
520
+ exports.normalizeColor = normalizeColor;
417
521
  exports.normalizeDocument = normalizeDocument;
418
522
  exports.normalizeEffect = normalizeEffect;
419
523
  exports.normalizeElement = normalizeElement;
@@ -424,6 +528,9 @@ exports.normalizeInnerShadow = normalizeInnerShadow;
424
528
  exports.normalizeOuterShadow = normalizeOuterShadow;
425
529
  exports.normalizeOutline = normalizeOutline;
426
530
  exports.normalizeShadow = normalizeShadow;
531
+ exports.normalizeSoftEdge = normalizeSoftEdge;
532
+ exports.normalizeStyle = normalizeStyle;
427
533
  exports.normalizeText = normalizeText;
428
534
  exports.normalizeTextContent = normalizeTextContent;
429
535
  exports.normalizeVideo = normalizeVideo;
536
+ exports.parseColor = parseColor;