-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathimporter.ts
More file actions
80 lines (70 loc) · 2.91 KB
/
Copy pathimporter.ts
File metadata and controls
80 lines (70 loc) · 2.91 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
import { CanvasSection, Sections } from 'types';
import { defaultImageSectionConfig } from './default-config';
import { v4 as uuid } from 'uuid';
import type { Element } from 'hast';
import { deepCopy } from 'utils/deepCopy';
const _updateBorderConfig = (params: URLSearchParams, config: any): any => {
config['type'] = params.get('type') || config['type'];
config['height'] = params.get('height')
? parseInt(params.get('height') as string, 10)
: config['height'];
config['section'] = params.get('section') || config['section'];
config['reversal'] = params.get('reversal') === 'true' || false;
config['color'] = {
type: params.get('color') || config['color']['type'],
theme: params.get('theme') || config['color']['theme'],
};
config['text'] = params.get('text') || config['text'];
config['fontSize'] = params.get('fontSize')
? parseInt(params.get('fontSize') as string, 10)
: config['fontSize'];
config['fontColor'] = params.get('fontColor') || config['fontColor'];
config['fontAlign'] = params.get('fontAlign')
? parseInt(params.get('fontAlign') as string, 10)
: config['fontAlign'];
config['fontAlignY'] = params.get('fontAlignY')
? parseInt(params.get('fontAlignY') as string, 10)
: config['fontAlignY'];
config['rotate'] = params.get('rotate')
? parseInt(params.get('rotate') as string, 10)
: config['rotate'];
config['stroke'] = params.get('stroke') || config['stroke'];
config['strokeWidth'] = params.get('strokeWidth')
? parseInt(params.get('strokeWidth') as string, 10)
: config['strokeWidth'];
config['animation'] = params.get('animation') || config['animation'];
config['desc'] = params.get('desc') || config['desc'];
config['descSize'] = params.get('descSize')
? parseInt(params.get('descSize') as string, 10)
: config['descSize'];
config['descAlign'] = params.get('descAlign')
? parseInt(params.get('descAlign') as string, 10)
: config['descAlign'];
config['descAlignY'] = params.get('descAlignY')
? parseInt(params.get('descAlignY') as string, 10)
: config['descAlignY'];
config['textBg'] = params.get('textBg') === 'true' || false;
return config;
};
const borderImporter = (borderElement: Element): CanvasSection | null => {
const defaultConfig = deepCopy(defaultImageSectionConfig);
if (borderElement.children.length === 0) return null;
const image = borderElement.children.find(
child => child.type === 'element' && child.tagName === 'img'
) as Element;
if (!image || !image.properties || !image.properties.src) return null;
const src = image.properties.src as string;
const url = new URL(src);
const params = url.searchParams;
defaultImageSectionConfig.props.content.borders['capsule-render'] =
_updateBorderConfig(
params,
defaultConfig.props.content.borders['capsule-render']
);
return {
id: uuid(),
type: Sections.BORDER,
...defaultConfig,
};
};
export { borderImporter };