1 | import { __assign } from "tslib";
|
2 | import { N30 } from '../../utils/colors';
|
3 | export var defaultAttrs = {
|
4 | id: { default: '' },
|
5 | type: { default: 'file' },
|
6 | collection: { default: '' },
|
7 | occurrenceKey: { default: null },
|
8 | width: { default: null },
|
9 | height: { default: null },
|
10 | url: { default: null },
|
11 | __fileName: { default: null },
|
12 | __fileSize: { default: null },
|
13 | __fileMimeType: { default: null },
|
14 | __displayType: { default: null },
|
15 | __contextId: { default: null },
|
16 | __external: { default: false },
|
17 | };
|
18 | export var defaultAttrsWithAltText = __assign(__assign({}, defaultAttrs), { alt: { default: null } });
|
19 | export var createMediaSpec = function (attributes) { return ({
|
20 | selectable: true,
|
21 | attrs: attributes,
|
22 | parseDOM: [
|
23 | {
|
24 | tag: 'div[data-node-type="media"]',
|
25 | getAttrs: function (dom) {
|
26 | var attrs = {};
|
27 | if (attributes) {
|
28 | Object.keys(attributes).forEach(function (k) {
|
29 | var key = camelCaseToKebabCase(k).replace(/^__/, '');
|
30 | var value = dom.getAttribute("data-" + key) || '';
|
31 | if (value) {
|
32 | attrs[k] = value;
|
33 | }
|
34 | });
|
35 | }
|
36 |
|
37 | if (attrs.__fileSize) {
|
38 | attrs.__fileSize = +attrs.__fileSize;
|
39 | }
|
40 | var width = Number(attrs.width);
|
41 | if (typeof width !== 'undefined' && !isNaN(width)) {
|
42 | attrs.width = width;
|
43 | }
|
44 | var height = Number(attrs.height);
|
45 | if (typeof height !== 'undefined' && !isNaN(height)) {
|
46 | attrs.height = height;
|
47 | }
|
48 | return attrs;
|
49 | },
|
50 | },
|
51 |
|
52 | {
|
53 | tag: 'img[src^="data:image"]',
|
54 | ignore: true,
|
55 | },
|
56 | {
|
57 | tag: 'img',
|
58 | getAttrs: function (dom) {
|
59 | return {
|
60 | type: 'external',
|
61 | url: dom.getAttribute('src') || '',
|
62 | };
|
63 | },
|
64 | },
|
65 | ],
|
66 | toDOM: function (node) {
|
67 | var attrs = {
|
68 | 'data-id': node.attrs.id,
|
69 | 'data-node-type': 'media',
|
70 | 'data-type': node.attrs.type,
|
71 | 'data-collection': node.attrs.collection,
|
72 | 'data-occurrence-key': node.attrs.occurrenceKey,
|
73 | 'data-width': node.attrs.width,
|
74 | 'data-height': node.attrs.height,
|
75 | 'data-url': node.attrs.url,
|
76 | 'data-alt': node.attrs.alt,
|
77 |
|
78 |
|
79 |
|
80 | title: 'Attachment',
|
81 |
|
82 |
|
83 | style: "display: inline-block; border-radius: 3px; background: " + N30 + "; box-shadow: 0 1px 1px rgba(9, 30, 66, 0.2), 0 0 1px 0 rgba(9, 30, 66, 0.24);",
|
84 | };
|
85 | copyPrivateAttributes(node.attrs, attrs, function (key) { return "data-" + camelCaseToKebabCase(key.slice(2)); });
|
86 | return ['div', attrs];
|
87 | },
|
88 | }); };
|
89 | export var media = createMediaSpec(defaultAttrs);
|
90 | export var mediaWithAltText = createMediaSpec(defaultAttrsWithAltText);
|
91 | export var camelCaseToKebabCase = function (str) {
|
92 | return str.replace(/([^A-Z]+)([A-Z])/g, function (_, x, y) { return x + "-" + y.toLowerCase(); });
|
93 | };
|
94 | export var copyPrivateAttributes = function (from, to, map) {
|
95 | if (media.attrs) {
|
96 | Object.keys(media.attrs).forEach(function (key) {
|
97 | if (key[0] === '_' && key[1] === '_' && from[key]) {
|
98 | to[map ? map(key) : key] = from[key];
|
99 | }
|
100 | });
|
101 | }
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | var optionalAttributes = ['occurrenceKey', 'width', 'height', 'url', 'alt'];
|
108 | var externalOnlyAttributes = ['type', 'url', 'width', 'height'];
|
109 | export var toJSON = function (node) { return ({
|
110 | attrs: Object.keys(node.attrs)
|
111 | .filter(function (key) { return !(key[0] === '_' && key[1] === '_'); })
|
112 | .reduce(function (obj, key) {
|
113 | if (node.attrs.type === 'external' &&
|
114 | externalOnlyAttributes.indexOf(key) === -1) {
|
115 | return obj;
|
116 | }
|
117 | if (optionalAttributes.indexOf(key) > -1 &&
|
118 | (node.attrs[key] === null || node.attrs[key] === '')) {
|
119 | return obj;
|
120 | }
|
121 | if (['width', 'height'].indexOf(key) !== -1) {
|
122 | obj[key] = Number(node.attrs[key]);
|
123 | return obj;
|
124 | }
|
125 | obj[key] = node.attrs[key];
|
126 | return obj;
|
127 | }, {}),
|
128 | }); };
|
129 |
|
\ | No newline at end of file |