-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathimporter.ts
More file actions
33 lines (28 loc) · 1.15 KB
/
Copy pathimporter.ts
File metadata and controls
33 lines (28 loc) · 1.15 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
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 imageImporter = (imageElement: Element): CanvasSection | null => {
const defaultConfig = deepCopy(defaultImageSectionConfig);
defaultConfig.props.styles.align = imageElement.properties?.align || 'center';
if (imageElement.tagName === 'img') {
defaultConfig.props.styles.float = imageElement.properties?.align || 'none';
defaultConfig.props.styles.height = imageElement.properties?.height || 200;
defaultConfig.props.content.url = imageElement.properties?.src || '';
} else if (imageElement.tagName === 'div') {
const image = imageElement.children.find(child => {
return (child as Element).tagName === 'img';
}) as Element;
defaultConfig.props.styles.height = image.properties?.height || 200;
defaultConfig.props.content.url = image.properties?.src || '';
} else {
return null;
}
return {
id: uuid(),
type: Sections.IMAGE,
...defaultConfig,
};
};
export { imageImporter };