@quadrats/common 0.7.7 → 1.0.0
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/accordion/constants.d.ts +5 -0
- package/accordion/constants.js +10 -0
- package/accordion/createAccordion.d.ts +6 -0
- package/accordion/createAccordion.js +62 -0
- package/accordion/index.cjs.js +76 -0
- package/accordion/index.d.ts +3 -0
- package/accordion/index.js +2 -0
- package/accordion/package.json +7 -0
- package/accordion/typings.d.ts +18 -0
- package/align/constants.d.ts +1 -0
- package/align/constants.js +3 -0
- package/align/createAlign.d.ts +9 -0
- package/align/createAlign.js +41 -0
- package/align/index.cjs.js +45 -0
- package/align/index.d.ts +3 -0
- package/align/index.js +2 -0
- package/align/package.json +7 -0
- package/align/typings.d.ts +1 -0
- package/card/constants.d.ts +6 -0
- package/card/constants.js +11 -0
- package/card/createCard.d.ts +15 -0
- package/card/createCard.js +147 -0
- package/card/getFilesFromInput.d.ts +4 -0
- package/card/index.cjs.js +163 -0
- package/card/index.d.ts +3 -0
- package/card/index.js +2 -0
- package/card/package.json +7 -0
- package/card/typings.d.ts +73 -0
- package/carousel/_virtual/_tslib.js +33 -0
- package/carousel/constants.d.ts +6 -0
- package/carousel/constants.js +11 -0
- package/carousel/createCarousel.d.ts +16 -0
- package/carousel/createCarousel.js +127 -0
- package/carousel/getFilesFromInput.d.ts +4 -0
- package/carousel/getFilesFromInput.js +30 -0
- package/carousel/index.cjs.js +202 -0
- package/carousel/index.d.ts +3 -0
- package/carousel/index.js +2 -0
- package/carousel/package.json +7 -0
- package/carousel/typings.d.ts +58 -0
- package/embed/constants.d.ts +1 -0
- package/embed/constants.js +2 -1
- package/embed/createEmbed.js +21 -4
- package/embed/index.cjs.js +26 -9
- package/embed/index.js +1 -1
- package/embed/serializeEmbedCode.d.ts +1 -1
- package/embed/serializeEmbedCode.js +5 -7
- package/embed/strategies/podcast-apple/index.cjs.js +1 -1
- package/embed/strategies/podcast-apple/index.js +1 -1
- package/embed/typings.d.ts +16 -1
- package/file-uploader/constants.d.ts +1 -0
- package/file-uploader/constants.js +2 -1
- package/file-uploader/createFileUploader.js +20 -2
- package/file-uploader/getFilesFromInput.js +3 -0
- package/file-uploader/index.cjs.js +23 -0
- package/file-uploader/index.js +1 -1
- package/file-uploader/typings.d.ts +8 -2
- package/heading/typings.d.ts +1 -0
- package/image/createImage.js +12 -11
- package/image/getImageFigureElementCommonProps.d.ts +2 -0
- package/image/getImageFigureElementCommonProps.js +28 -2
- package/image/index.cjs.js +40 -13
- package/image/typings.d.ts +2 -0
- package/list/createList.d.ts +1 -0
- package/list/createList.js +3 -0
- package/list/index.cjs.js +3 -0
- package/list/typings.d.ts +1 -0
- package/package.json +4 -4
- package/paragraph/createParagraph.d.ts +2 -0
- package/paragraph/createParagraph.js +24 -0
- package/paragraph/index.cjs.js +26 -0
- package/paragraph/index.d.ts +2 -0
- package/paragraph/index.js +1 -0
- package/paragraph/package.json +7 -0
- package/paragraph/typings.d.ts +9 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@quadrats/core');
|
|
4
|
+
|
|
5
|
+
const CAROUSEL_TYPE = 'carousel';
|
|
6
|
+
const CAROUSEL_IMAGES_TYPE = 'carousel_images';
|
|
7
|
+
const CAROUSEL_CAPTION_TYPE = 'carousel_caption';
|
|
8
|
+
const CAROUSEL_PLACEHOLDER_TYPE = 'carousel_placeholder';
|
|
9
|
+
const CAROUSEL_TYPES = {
|
|
10
|
+
carousel: CAROUSEL_TYPE,
|
|
11
|
+
carousel_images: CAROUSEL_IMAGES_TYPE,
|
|
12
|
+
carousel_caption: CAROUSEL_CAPTION_TYPE,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/******************************************************************************
|
|
16
|
+
Copyright (c) Microsoft Corporation.
|
|
17
|
+
|
|
18
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
19
|
+
purpose with or without fee is hereby granted.
|
|
20
|
+
|
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
22
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
23
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
24
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
25
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
26
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
27
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
28
|
+
***************************************************************************** */
|
|
29
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
33
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
34
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
35
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
36
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
37
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
38
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
43
|
+
var e = new Error(message);
|
|
44
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
function getFilesFromInput(options) {
|
|
48
|
+
const { accept } = options;
|
|
49
|
+
return new Promise((resolve) => {
|
|
50
|
+
const inputEl = document.createElement('input');
|
|
51
|
+
if (accept) {
|
|
52
|
+
inputEl.accept = accept.join(',');
|
|
53
|
+
}
|
|
54
|
+
inputEl.multiple = true;
|
|
55
|
+
inputEl.type = 'file';
|
|
56
|
+
inputEl.addEventListener('cancel', () => {
|
|
57
|
+
resolve(undefined);
|
|
58
|
+
});
|
|
59
|
+
inputEl.addEventListener('change', () => {
|
|
60
|
+
const { files: fileList } = inputEl;
|
|
61
|
+
if (!fileList || !fileList.length) {
|
|
62
|
+
resolve(undefined);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const files = [];
|
|
66
|
+
for (const file of fileList) {
|
|
67
|
+
files.push(file);
|
|
68
|
+
}
|
|
69
|
+
resolve(files);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
inputEl.click();
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function createCarousel(options) {
|
|
77
|
+
const { types: typesOptions, accept: acceptOptions, ratio, maxLength: maxLengthOptions, limitSize: limitSizeOptions, confirmModal = true, getBody, getHeaders, getUrl, uploader, } = options;
|
|
78
|
+
const types = Object.assign(Object.assign({}, CAROUSEL_TYPES), typesOptions);
|
|
79
|
+
const maxLength = maxLengthOptions || 10;
|
|
80
|
+
const limitSize = limitSizeOptions || 5;
|
|
81
|
+
const accept = acceptOptions || ['image/jpeg', 'image/jpg', 'image/png'];
|
|
82
|
+
const selectFiles = () => __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
const files = yield getFilesFromInput({ accept });
|
|
84
|
+
return files;
|
|
85
|
+
});
|
|
86
|
+
const insertCarouselPlaceholder = (editor) => {
|
|
87
|
+
const carouselPlaceholderElement = {
|
|
88
|
+
type: CAROUSEL_PLACEHOLDER_TYPE,
|
|
89
|
+
ratio,
|
|
90
|
+
children: [{ text: '' }],
|
|
91
|
+
};
|
|
92
|
+
core.Editor.withoutNormalizing(editor, () => {
|
|
93
|
+
core.Transforms.insertNodes(editor, carouselPlaceholderElement);
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
const removeCarouselPlaceholder = (editor) => {
|
|
97
|
+
core.Transforms.removeNodes(editor, {
|
|
98
|
+
match: (node) => core.Element.isElement(node) && node.type === CAROUSEL_PLACEHOLDER_TYPE,
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
const createCarouselElement = ({ items }) => {
|
|
102
|
+
const carouselImagesElement = {
|
|
103
|
+
type: types.carousel_images,
|
|
104
|
+
images: items.map((i) => i.url),
|
|
105
|
+
ratio,
|
|
106
|
+
children: [{ text: '' }],
|
|
107
|
+
};
|
|
108
|
+
const carouselCaptionElement = {
|
|
109
|
+
type: types.carousel_caption,
|
|
110
|
+
captions: items.map((i) => i.caption),
|
|
111
|
+
children: [{ text: '' }],
|
|
112
|
+
};
|
|
113
|
+
return {
|
|
114
|
+
type: types.carousel,
|
|
115
|
+
confirmModal,
|
|
116
|
+
items,
|
|
117
|
+
children: [carouselImagesElement, carouselCaptionElement],
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
const insertCarousel = ({ editor, items }) => {
|
|
121
|
+
core.Transforms.insertNodes(editor, [createCarouselElement({ items }), core.createParagraphElement()]);
|
|
122
|
+
};
|
|
123
|
+
const updateCarouselElement = ({ editor, items, path }) => {
|
|
124
|
+
core.Transforms.setNodes(editor, { items }, { at: path });
|
|
125
|
+
const imagesEntries = core.Editor.nodes(editor, {
|
|
126
|
+
at: path,
|
|
127
|
+
match: (node) => core.Element.isElement(node) && node.type === types.carousel_images,
|
|
128
|
+
mode: 'all',
|
|
129
|
+
});
|
|
130
|
+
const imagesNode = imagesEntries.next().value;
|
|
131
|
+
const captionEntries = core.Editor.nodes(editor, {
|
|
132
|
+
at: path,
|
|
133
|
+
match: (node) => core.Element.isElement(node) && node.type === types.carousel_caption,
|
|
134
|
+
mode: 'all',
|
|
135
|
+
});
|
|
136
|
+
const captionNode = captionEntries.next().value;
|
|
137
|
+
if (imagesNode) {
|
|
138
|
+
const [, imagesPath] = imagesNode;
|
|
139
|
+
core.Transforms.setNodes(editor, { images: items.map((i) => i.url) }, { at: imagesPath });
|
|
140
|
+
}
|
|
141
|
+
if (captionNode) {
|
|
142
|
+
const [, captionPath] = captionNode;
|
|
143
|
+
core.Transforms.setNodes(editor, { captions: items.map((i) => i.caption) }, {
|
|
144
|
+
at: captionPath,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
return {
|
|
149
|
+
types,
|
|
150
|
+
accept,
|
|
151
|
+
ratio,
|
|
152
|
+
maxLength,
|
|
153
|
+
limitSize,
|
|
154
|
+
confirmModal,
|
|
155
|
+
selectFiles,
|
|
156
|
+
insertCarouselPlaceholder,
|
|
157
|
+
removeCarouselPlaceholder,
|
|
158
|
+
createCarouselElement,
|
|
159
|
+
insertCarousel,
|
|
160
|
+
updateCarouselElement,
|
|
161
|
+
getBody,
|
|
162
|
+
getHeaders,
|
|
163
|
+
getUrl,
|
|
164
|
+
uploader,
|
|
165
|
+
with(editor) {
|
|
166
|
+
const { normalizeNode } = editor;
|
|
167
|
+
editor.normalizeNode = (entry) => {
|
|
168
|
+
const [node, path] = entry;
|
|
169
|
+
if (core.Element.isElement(node)) {
|
|
170
|
+
const type = node.type;
|
|
171
|
+
if (type === types.carousel) {
|
|
172
|
+
for (const [child, childPath] of core.Node.children(editor, path)) {
|
|
173
|
+
if (core.Element.isElement(child) &&
|
|
174
|
+
child.type !== types.carousel_caption &&
|
|
175
|
+
child.type !== types.carousel_images) {
|
|
176
|
+
core.Transforms.removeNodes(editor, { at: childPath });
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else if (type === types.carousel_caption || type === types.carousel_images) {
|
|
182
|
+
for (const [child, childPath] of core.Node.children(editor, path)) {
|
|
183
|
+
if (core.Element.isElement(child)) {
|
|
184
|
+
core.Transforms.removeNodes(editor, { at: childPath });
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
normalizeNode(entry);
|
|
191
|
+
};
|
|
192
|
+
return editor;
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
exports.CAROUSEL_CAPTION_TYPE = CAROUSEL_CAPTION_TYPE;
|
|
198
|
+
exports.CAROUSEL_IMAGES_TYPE = CAROUSEL_IMAGES_TYPE;
|
|
199
|
+
exports.CAROUSEL_PLACEHOLDER_TYPE = CAROUSEL_PLACEHOLDER_TYPE;
|
|
200
|
+
exports.CAROUSEL_TYPE = CAROUSEL_TYPE;
|
|
201
|
+
exports.CAROUSEL_TYPES = CAROUSEL_TYPES;
|
|
202
|
+
exports.createCarousel = createCarousel;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Editor, Withable, QuadratsElement, WithElementType, Text, Path } from '@quadrats/core';
|
|
2
|
+
import { ImageAccept, FileUploaderGetBody, FileUploaderGetHeaders, FileUploaderGetUrl, FileUploaderImplement } from '@quadrats/common/file-uploader';
|
|
3
|
+
export type CarouselTypeKey = 'carousel';
|
|
4
|
+
export type CarouselImagesTypeKey = 'carousel_images';
|
|
5
|
+
export type CarouselCaptionTypeKey = 'carousel_caption';
|
|
6
|
+
export type CarouselTypes = Record<CarouselTypeKey | CarouselImagesTypeKey | CarouselCaptionTypeKey, string>;
|
|
7
|
+
export interface CarouselFieldArrayItem {
|
|
8
|
+
file: File;
|
|
9
|
+
progress: number;
|
|
10
|
+
preview: string;
|
|
11
|
+
url: string;
|
|
12
|
+
caption: string;
|
|
13
|
+
isError?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface CarouselElement extends QuadratsElement, WithElementType {
|
|
16
|
+
confirmModal: boolean;
|
|
17
|
+
items: CarouselFieldArrayItem[];
|
|
18
|
+
}
|
|
19
|
+
export interface CarouselImagesElement extends QuadratsElement, WithElementType {
|
|
20
|
+
children: [Text];
|
|
21
|
+
ratio?: [number, number];
|
|
22
|
+
images: string[];
|
|
23
|
+
}
|
|
24
|
+
export interface CarouselCaptionElement extends QuadratsElement, WithElementType {
|
|
25
|
+
children: [Text];
|
|
26
|
+
captions: string[];
|
|
27
|
+
}
|
|
28
|
+
export interface CarouselPlaceholderElement extends QuadratsElement, WithElementType {
|
|
29
|
+
ratio?: [number, number];
|
|
30
|
+
children: [Text];
|
|
31
|
+
}
|
|
32
|
+
export interface Carousel<T extends Editor = Editor> extends Withable {
|
|
33
|
+
types: CarouselTypes;
|
|
34
|
+
insertCarouselPlaceholder(editor: T): void;
|
|
35
|
+
removeCarouselPlaceholder(editor: T): void;
|
|
36
|
+
createCarouselElement({ items }: {
|
|
37
|
+
items: CarouselFieldArrayItem[];
|
|
38
|
+
}): CarouselElement;
|
|
39
|
+
insertCarousel({ editor, items }: {
|
|
40
|
+
editor: T;
|
|
41
|
+
items: CarouselFieldArrayItem[];
|
|
42
|
+
}): void;
|
|
43
|
+
updateCarouselElement({ editor, items, path }: {
|
|
44
|
+
editor: T;
|
|
45
|
+
items: CarouselFieldArrayItem[];
|
|
46
|
+
path: Path;
|
|
47
|
+
}): void;
|
|
48
|
+
accept: ImageAccept[];
|
|
49
|
+
ratio?: [number, number];
|
|
50
|
+
maxLength: number;
|
|
51
|
+
limitSize: number;
|
|
52
|
+
confirmModal: boolean;
|
|
53
|
+
selectFiles(editor: T): Promise<File[] | undefined>;
|
|
54
|
+
getBody: FileUploaderGetBody;
|
|
55
|
+
getHeaders?: FileUploaderGetHeaders;
|
|
56
|
+
getUrl: FileUploaderGetUrl;
|
|
57
|
+
uploader?: FileUploaderImplement;
|
|
58
|
+
}
|
package/embed/constants.d.ts
CHANGED
package/embed/constants.js
CHANGED
package/embed/createEmbed.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { PARAGRAPH_TYPE, Element, Transforms, normalizeVoidElementChildren, isAboveBlockEmpty } from '@quadrats/core';
|
|
2
|
-
import { EMBED_TYPE } from './constants.js';
|
|
1
|
+
import { PARAGRAPH_TYPE, Element, Transforms, normalizeVoidElementChildren, Editor, isAboveBlockEmpty } from '@quadrats/core';
|
|
2
|
+
import { EMBED_TYPE, EMBED_PLACEHOLDER_TYPE } from './constants.js';
|
|
3
3
|
import { serializeEmbedCode } from './serializeEmbedCode.js';
|
|
4
4
|
|
|
5
5
|
function createEmbed(options) {
|
|
6
6
|
const { type = EMBED_TYPE, strategies } = options;
|
|
7
|
-
const insertEmbed = (editor,
|
|
7
|
+
const insertEmbed = (editor, provider, embedCode, defaultNode = PARAGRAPH_TYPE) => {
|
|
8
8
|
var _a;
|
|
9
|
-
const result = serializeEmbedCode(embedCode, strategies,
|
|
9
|
+
const result = serializeEmbedCode(embedCode, strategies, provider);
|
|
10
10
|
if (result) {
|
|
11
11
|
const [provider, data] = result;
|
|
12
12
|
const embedElement = Object.assign(Object.assign({}, data), { type, provider, children: [{ text: '' }] });
|
|
@@ -23,10 +23,27 @@ function createEmbed(options) {
|
|
|
23
23
|
Transforms.move(editor);
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
|
+
const insertEmbedPlaceholder = (editor, provider) => {
|
|
27
|
+
const embedPlaceholderElement = {
|
|
28
|
+
type: EMBED_PLACEHOLDER_TYPE,
|
|
29
|
+
provider,
|
|
30
|
+
children: [{ text: '' }],
|
|
31
|
+
};
|
|
32
|
+
Editor.withoutNormalizing(editor, () => {
|
|
33
|
+
Transforms.insertNodes(editor, embedPlaceholderElement);
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
const removeEmbedPlaceholder = (editor) => {
|
|
37
|
+
Transforms.removeNodes(editor, {
|
|
38
|
+
match: node => Element.isElement(node) && node.type === EMBED_PLACEHOLDER_TYPE,
|
|
39
|
+
});
|
|
40
|
+
};
|
|
26
41
|
return {
|
|
27
42
|
type,
|
|
28
43
|
strategies,
|
|
29
44
|
insertEmbed,
|
|
45
|
+
insertEmbedPlaceholder,
|
|
46
|
+
removeEmbedPlaceholder,
|
|
30
47
|
with(editor) {
|
|
31
48
|
const { isVoid, normalizeNode } = editor;
|
|
32
49
|
editor.isVoid = element => element.type === type || isVoid(element);
|
package/embed/index.cjs.js
CHANGED
|
@@ -3,14 +3,13 @@
|
|
|
3
3
|
var core = require('@quadrats/core');
|
|
4
4
|
|
|
5
5
|
const EMBED_TYPE = 'embed';
|
|
6
|
+
const EMBED_PLACEHOLDER_TYPE = 'embed_placeholder';
|
|
6
7
|
|
|
7
|
-
function serializeEmbedCode(embedCode, strategies,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return [provider, data];
|
|
13
|
-
}
|
|
8
|
+
function serializeEmbedCode(embedCode, strategies, provider) {
|
|
9
|
+
const strategy = strategies[provider];
|
|
10
|
+
const data = strategy.serialize(embedCode);
|
|
11
|
+
if (data) {
|
|
12
|
+
return [provider, data];
|
|
14
13
|
}
|
|
15
14
|
}
|
|
16
15
|
|
|
@@ -24,9 +23,9 @@ function deserializeEmbedElementToData(element, strategies) {
|
|
|
24
23
|
|
|
25
24
|
function createEmbed(options) {
|
|
26
25
|
const { type = EMBED_TYPE, strategies } = options;
|
|
27
|
-
const insertEmbed = (editor,
|
|
26
|
+
const insertEmbed = (editor, provider, embedCode, defaultNode = core.PARAGRAPH_TYPE) => {
|
|
28
27
|
var _a;
|
|
29
|
-
const result = serializeEmbedCode(embedCode, strategies,
|
|
28
|
+
const result = serializeEmbedCode(embedCode, strategies, provider);
|
|
30
29
|
if (result) {
|
|
31
30
|
const [provider, data] = result;
|
|
32
31
|
const embedElement = Object.assign(Object.assign({}, data), { type, provider, children: [{ text: '' }] });
|
|
@@ -43,10 +42,27 @@ function createEmbed(options) {
|
|
|
43
42
|
core.Transforms.move(editor);
|
|
44
43
|
}
|
|
45
44
|
};
|
|
45
|
+
const insertEmbedPlaceholder = (editor, provider) => {
|
|
46
|
+
const embedPlaceholderElement = {
|
|
47
|
+
type: EMBED_PLACEHOLDER_TYPE,
|
|
48
|
+
provider,
|
|
49
|
+
children: [{ text: '' }],
|
|
50
|
+
};
|
|
51
|
+
core.Editor.withoutNormalizing(editor, () => {
|
|
52
|
+
core.Transforms.insertNodes(editor, embedPlaceholderElement);
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
const removeEmbedPlaceholder = (editor) => {
|
|
56
|
+
core.Transforms.removeNodes(editor, {
|
|
57
|
+
match: node => core.Element.isElement(node) && node.type === EMBED_PLACEHOLDER_TYPE,
|
|
58
|
+
});
|
|
59
|
+
};
|
|
46
60
|
return {
|
|
47
61
|
type,
|
|
48
62
|
strategies,
|
|
49
63
|
insertEmbed,
|
|
64
|
+
insertEmbedPlaceholder,
|
|
65
|
+
removeEmbedPlaceholder,
|
|
50
66
|
with(editor) {
|
|
51
67
|
const { isVoid, normalizeNode } = editor;
|
|
52
68
|
editor.isVoid = element => element.type === type || isVoid(element);
|
|
@@ -70,6 +86,7 @@ function createEmbed(options) {
|
|
|
70
86
|
};
|
|
71
87
|
}
|
|
72
88
|
|
|
89
|
+
exports.EMBED_PLACEHOLDER_TYPE = EMBED_PLACEHOLDER_TYPE;
|
|
73
90
|
exports.EMBED_TYPE = EMBED_TYPE;
|
|
74
91
|
exports.createEmbed = createEmbed;
|
|
75
92
|
exports.deserializeEmbedElementToData = deserializeEmbedElementToData;
|
package/embed/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { EMBED_TYPE } from './constants.js';
|
|
1
|
+
export { EMBED_PLACEHOLDER_TYPE, EMBED_TYPE } from './constants.js';
|
|
2
2
|
export { serializeEmbedCode } from './serializeEmbedCode.js';
|
|
3
3
|
export { deserializeEmbedElementToData } from './deserializeEmbedElementToData.js';
|
|
4
4
|
export { createEmbed } from './createEmbed.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { EmbedStrategies } from './typings';
|
|
2
|
-
export declare function serializeEmbedCode<P extends string>(embedCode: string, strategies: EmbedStrategies<P>,
|
|
2
|
+
export declare function serializeEmbedCode<P extends string>(embedCode: string, strategies: EmbedStrategies<P>, provider: P): [P, any] | undefined;
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
function serializeEmbedCode(embedCode, strategies,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return [provider, data];
|
|
7
|
-
}
|
|
1
|
+
function serializeEmbedCode(embedCode, strategies, provider) {
|
|
2
|
+
const strategy = strategies[provider];
|
|
3
|
+
const data = strategy.serialize(embedCode);
|
|
4
|
+
if (data) {
|
|
5
|
+
return [provider, data];
|
|
8
6
|
}
|
|
9
7
|
}
|
|
10
8
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
const PodcastAppleEmbedStrategy = {
|
|
5
5
|
serialize: (embedCode) => {
|
|
6
6
|
var _a, _b;
|
|
7
|
-
const result = (_b = (_a = /^https:\/\/embed.podcasts.apple.com\/([\w-]*)\/podcast\/[\S]*\/id([\
|
|
7
|
+
const result = (_b = (_a = /^https:\/\/embed.podcasts.apple.com\/([\w-]*)\/podcast\/[\S]*\/id([\S]*)/i.exec(embedCode)) !== null && _a !== void 0 ? _a : /^https:\/\/embed.podcasts.apple.com\/([\w-]*)\/podcast\/id([\S]*)/i.exec(embedCode)) !== null && _b !== void 0 ? _b : /^https:\/\/podcasts.apple.com\/([\w-]*)\/podcast\/[\S]*\/id([\S]*)/i.exec(embedCode);
|
|
8
8
|
if (result) {
|
|
9
9
|
const [, language, contextId] = result;
|
|
10
10
|
return {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
const PodcastAppleEmbedStrategy = {
|
|
3
3
|
serialize: (embedCode) => {
|
|
4
4
|
var _a, _b;
|
|
5
|
-
const result = (_b = (_a = /^https:\/\/embed.podcasts.apple.com\/([\w-]*)\/podcast\/[\S]*\/id([\
|
|
5
|
+
const result = (_b = (_a = /^https:\/\/embed.podcasts.apple.com\/([\w-]*)\/podcast\/[\S]*\/id([\S]*)/i.exec(embedCode)) !== null && _a !== void 0 ? _a : /^https:\/\/embed.podcasts.apple.com\/([\w-]*)\/podcast\/id([\S]*)/i.exec(embedCode)) !== null && _b !== void 0 ? _b : /^https:\/\/podcasts.apple.com\/([\w-]*)\/podcast\/[\S]*\/id([\S]*)/i.exec(embedCode);
|
|
6
6
|
if (result) {
|
|
7
7
|
const [, language, contextId] = result;
|
|
8
8
|
return {
|
package/embed/typings.d.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import { Editor, QuadratsElement, Node, Text, Withable, WithElementType } from '@quadrats/core';
|
|
2
2
|
export interface EmbedElement extends QuadratsElement, WithElementType {
|
|
3
|
+
/**
|
|
4
|
+
* The embed provider.
|
|
5
|
+
*
|
|
6
|
+
* e.g. If you embed a youtube video, then you can use `youtube` as provider.
|
|
7
|
+
*/
|
|
8
|
+
provider: string;
|
|
9
|
+
/**
|
|
10
|
+
* The embed alignment. default is `left`.
|
|
11
|
+
*/
|
|
12
|
+
align?: 'left' | 'center' | 'right';
|
|
13
|
+
children: [Text];
|
|
14
|
+
}
|
|
15
|
+
export interface EmbedPlaceholderElement extends QuadratsElement, WithElementType {
|
|
3
16
|
/**
|
|
4
17
|
* The embed provider.
|
|
5
18
|
*
|
|
@@ -22,5 +35,7 @@ export interface EmbedStrategy<EmbedData extends Record<string, unknown>, Render
|
|
|
22
35
|
export type EmbedStrategies<Provider extends string> = Record<Provider, EmbedStrategy<any, any>>;
|
|
23
36
|
export interface Embed<P extends string, T extends Editor = Editor> extends WithElementType, Withable {
|
|
24
37
|
strategies: EmbedStrategies<P>;
|
|
25
|
-
insertEmbed(editor: T,
|
|
38
|
+
insertEmbed(editor: T, provider: P, embedCode: string, defaultNode?: Node | string): void;
|
|
39
|
+
insertEmbedPlaceholder(editor: T, provider: P): void;
|
|
40
|
+
removeEmbedPlaceholder(editor: T): void;
|
|
26
41
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __awaiter } from './_virtual/_tslib.js';
|
|
2
2
|
import { readFileAsDataURL } from '@quadrats/utils';
|
|
3
|
-
import { isAboveBlockEmpty, Transforms, createParagraphElement, HistoryEditor } from '@quadrats/core';
|
|
4
|
-
import { FILE_UPLOADER_TYPE } from './constants.js';
|
|
3
|
+
import { isAboveBlockEmpty, Transforms, createParagraphElement, Element, Editor, HistoryEditor } from '@quadrats/core';
|
|
4
|
+
import { FILE_UPLOADER_TYPE, FILE_UPLOADER_PLACEHOLDER_TYPE } from './constants.js';
|
|
5
5
|
import { getFilesFromInput } from './getFilesFromInput.js';
|
|
6
6
|
|
|
7
7
|
const createFileUploaderElementByType = type => (editor, file, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -71,12 +71,19 @@ function insertFileUploaderElement(editor, fileUploaderElement) {
|
|
|
71
71
|
function createFileUploader(options = {}) {
|
|
72
72
|
const { type = FILE_UPLOADER_TYPE } = options;
|
|
73
73
|
const createFileUploaderElement = createFileUploaderElementByType(type);
|
|
74
|
+
const removeUploaderPlaceholder = (editor) => {
|
|
75
|
+
Transforms.removeNodes(editor, {
|
|
76
|
+
match: node => Element.isElement(node) && node.type === FILE_UPLOADER_PLACEHOLDER_TYPE,
|
|
77
|
+
});
|
|
78
|
+
};
|
|
74
79
|
const upload = (editor, options) => __awaiter(this, void 0, void 0, function* () {
|
|
75
80
|
const { accept, multiple } = options;
|
|
76
81
|
const files = yield getFilesFromInput({ accept, multiple });
|
|
77
82
|
if (!files) {
|
|
83
|
+
removeUploaderPlaceholder(editor);
|
|
78
84
|
return;
|
|
79
85
|
}
|
|
86
|
+
removeUploaderPlaceholder(editor);
|
|
80
87
|
files.reduce((prev, file) => __awaiter(this, void 0, void 0, function* () {
|
|
81
88
|
yield prev;
|
|
82
89
|
return createFileUploaderElement(editor, file, options).then((fileUploaderElement) => {
|
|
@@ -84,10 +91,21 @@ function createFileUploader(options = {}) {
|
|
|
84
91
|
});
|
|
85
92
|
}), Promise.resolve());
|
|
86
93
|
});
|
|
94
|
+
const insertUploaderPlaceholder = (editor) => {
|
|
95
|
+
const uploaderPlaceholderElement = {
|
|
96
|
+
type: FILE_UPLOADER_PLACEHOLDER_TYPE,
|
|
97
|
+
children: [{ text: '' }],
|
|
98
|
+
};
|
|
99
|
+
Editor.withoutNormalizing(editor, () => {
|
|
100
|
+
Transforms.insertNodes(editor, uploaderPlaceholderElement);
|
|
101
|
+
});
|
|
102
|
+
};
|
|
87
103
|
return {
|
|
88
104
|
type,
|
|
89
105
|
createFileUploaderElement,
|
|
90
106
|
upload,
|
|
107
|
+
insertUploaderPlaceholder,
|
|
108
|
+
removeUploaderPlaceholder,
|
|
91
109
|
with(editor) {
|
|
92
110
|
return editor;
|
|
93
111
|
},
|
|
@@ -7,6 +7,9 @@ function getFilesFromInput(options = {}) {
|
|
|
7
7
|
}
|
|
8
8
|
inputEl.multiple = !!multiple;
|
|
9
9
|
inputEl.type = 'file';
|
|
10
|
+
inputEl.addEventListener('cancel', () => {
|
|
11
|
+
resolve(undefined);
|
|
12
|
+
});
|
|
10
13
|
inputEl.addEventListener('change', () => {
|
|
11
14
|
const { files: fileList } = inputEl;
|
|
12
15
|
if (!fileList || !fileList.length) {
|
|
@@ -4,6 +4,7 @@ var utils = require('@quadrats/utils');
|
|
|
4
4
|
var core = require('@quadrats/core');
|
|
5
5
|
|
|
6
6
|
const FILE_UPLOADER_TYPE = 'file_uploader';
|
|
7
|
+
const FILE_UPLOADER_PLACEHOLDER_TYPE = 'file_uploader_placeholder';
|
|
7
8
|
|
|
8
9
|
function getFilesFromInput(options = {}) {
|
|
9
10
|
const { accept, multiple } = options;
|
|
@@ -14,6 +15,9 @@ function getFilesFromInput(options = {}) {
|
|
|
14
15
|
}
|
|
15
16
|
inputEl.multiple = !!multiple;
|
|
16
17
|
inputEl.type = 'file';
|
|
18
|
+
inputEl.addEventListener('cancel', () => {
|
|
19
|
+
resolve(undefined);
|
|
20
|
+
});
|
|
17
21
|
inputEl.addEventListener('change', () => {
|
|
18
22
|
const { files: fileList } = inputEl;
|
|
19
23
|
if (!fileList || !fileList.length) {
|
|
@@ -130,12 +134,19 @@ function insertFileUploaderElement(editor, fileUploaderElement) {
|
|
|
130
134
|
function createFileUploader(options = {}) {
|
|
131
135
|
const { type = FILE_UPLOADER_TYPE } = options;
|
|
132
136
|
const createFileUploaderElement = createFileUploaderElementByType(type);
|
|
137
|
+
const removeUploaderPlaceholder = (editor) => {
|
|
138
|
+
core.Transforms.removeNodes(editor, {
|
|
139
|
+
match: node => core.Element.isElement(node) && node.type === FILE_UPLOADER_PLACEHOLDER_TYPE,
|
|
140
|
+
});
|
|
141
|
+
};
|
|
133
142
|
const upload = (editor, options) => __awaiter(this, void 0, void 0, function* () {
|
|
134
143
|
const { accept, multiple } = options;
|
|
135
144
|
const files = yield getFilesFromInput({ accept, multiple });
|
|
136
145
|
if (!files) {
|
|
146
|
+
removeUploaderPlaceholder(editor);
|
|
137
147
|
return;
|
|
138
148
|
}
|
|
149
|
+
removeUploaderPlaceholder(editor);
|
|
139
150
|
files.reduce((prev, file) => __awaiter(this, void 0, void 0, function* () {
|
|
140
151
|
yield prev;
|
|
141
152
|
return createFileUploaderElement(editor, file, options).then((fileUploaderElement) => {
|
|
@@ -143,16 +154,28 @@ function createFileUploader(options = {}) {
|
|
|
143
154
|
});
|
|
144
155
|
}), Promise.resolve());
|
|
145
156
|
});
|
|
157
|
+
const insertUploaderPlaceholder = (editor) => {
|
|
158
|
+
const uploaderPlaceholderElement = {
|
|
159
|
+
type: FILE_UPLOADER_PLACEHOLDER_TYPE,
|
|
160
|
+
children: [{ text: '' }],
|
|
161
|
+
};
|
|
162
|
+
core.Editor.withoutNormalizing(editor, () => {
|
|
163
|
+
core.Transforms.insertNodes(editor, uploaderPlaceholderElement);
|
|
164
|
+
});
|
|
165
|
+
};
|
|
146
166
|
return {
|
|
147
167
|
type,
|
|
148
168
|
createFileUploaderElement,
|
|
149
169
|
upload,
|
|
170
|
+
insertUploaderPlaceholder,
|
|
171
|
+
removeUploaderPlaceholder,
|
|
150
172
|
with(editor) {
|
|
151
173
|
return editor;
|
|
152
174
|
},
|
|
153
175
|
};
|
|
154
176
|
}
|
|
155
177
|
|
|
178
|
+
exports.FILE_UPLOADER_PLACEHOLDER_TYPE = FILE_UPLOADER_PLACEHOLDER_TYPE;
|
|
156
179
|
exports.FILE_UPLOADER_TYPE = FILE_UPLOADER_TYPE;
|
|
157
180
|
exports.createFileUploader = createFileUploader;
|
|
158
181
|
exports.createFileUploaderElementByType = createFileUploaderElementByType;
|
package/file-uploader/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { FILE_UPLOADER_TYPE } from './constants.js';
|
|
1
|
+
export { FILE_UPLOADER_PLACEHOLDER_TYPE, FILE_UPLOADER_TYPE } from './constants.js';
|
|
2
2
|
export { getFilesFromInput } from './getFilesFromInput.js';
|
|
3
3
|
export { createFileUploader, createFileUploaderElementByType, insertFileUploaderElement } from './createFileUploader.js';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Editor, QuadratsElement, Path, TransformsInsertNodesOptions, Withable } from '@quadrats/core';
|
|
1
|
+
import { Editor, QuadratsElement, Path, TransformsInsertNodesOptions, Withable, WithElementType, Text } from '@quadrats/core';
|
|
2
2
|
import { GetFilesFromInputOptions } from './getFilesFromInput';
|
|
3
|
+
export type ImageAccept = 'image/jpeg' | 'image/jpg' | 'image/png';
|
|
3
4
|
export interface XHRUploadHeaders {
|
|
4
5
|
[name: string]: string;
|
|
5
6
|
}
|
|
@@ -25,7 +26,7 @@ interface FileUploaderUploadImplementOnProgressArgs {
|
|
|
25
26
|
interface FileUploaderUploadImplement {
|
|
26
27
|
onprogress: ((options: FileUploaderUploadImplementOnProgressArgs) => void) | null;
|
|
27
28
|
}
|
|
28
|
-
interface FileUploaderImplement {
|
|
29
|
+
export interface FileUploaderImplement {
|
|
29
30
|
onload: (() => void) | null;
|
|
30
31
|
open: (method: string, url: string | URL) => void;
|
|
31
32
|
setRequestHeader: (key: string, value: string) => void;
|
|
@@ -34,6 +35,9 @@ interface FileUploaderImplement {
|
|
|
34
35
|
readonly response: any;
|
|
35
36
|
readonly upload: FileUploaderUploadImplement;
|
|
36
37
|
}
|
|
38
|
+
export interface UploaderPlaceholderElement extends QuadratsElement, WithElementType {
|
|
39
|
+
children: [Text];
|
|
40
|
+
}
|
|
37
41
|
export interface FileUploaderCreateFileUploaderElementOptions {
|
|
38
42
|
createElement: {
|
|
39
43
|
[mime in string]?: {
|
|
@@ -51,5 +55,7 @@ export interface FileUploader<T extends Editor = Editor> extends Withable {
|
|
|
51
55
|
type: string;
|
|
52
56
|
createFileUploaderElement(editor: T, file: File, options: FileUploaderCreateFileUploaderElementOptions): Promise<FileUploaderElement | undefined>;
|
|
53
57
|
upload(editor: T, options: FileUploaderUploadOptions): Promise<void>;
|
|
58
|
+
insertUploaderPlaceholder(editor: T): void;
|
|
59
|
+
removeUploaderPlaceholder(editor: T): void;
|
|
54
60
|
}
|
|
55
61
|
export {};
|