Skip to content

Commit 9f01382

Browse files
authored
Merge pull request #22 from webkom/image-test
Increase compatibility for images
2 parents 64fff3e + de34e91 commit 9f01382

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

example/src/App.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const App = () => (
1212
<Editor
1313
placeholder="Testing lego editor"
1414
imageUpload={file =>
15-
new Promise(resolve => resolve(URL.createObjectURL(file)))
15+
new Promise(resolve => resolve())
1616
}
1717
/>
1818
</div>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@webkom/lego-editor",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A React editor written in TS with slate.js for lego-webapp",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/components/Toolbar.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ export default class Toolbar extends React.Component<
219219
this.setState({ insertingImage: true });
220220
}
221221

222-
insertImage(image: Blob): void {
222+
insertImage(image: Blob, data?: Record<string, any>): void {
223223
const { editor } = this.props;
224224

225225
this.setState({ insertingImage: false });
226-
editor.command('insertImage', image);
226+
editor.command('insertImage', image, data);
227227
}
228228

229229
onClose(): void {

src/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ interface Props {
3333
onFocus?: () => void;
3434
autoFocus?: boolean;
3535
placeholder?: string;
36-
imageUpload: (file: Blob) => Promise<string>;
36+
imageUpload: (file: Blob) => Promise<Record<string, any>>;
3737
plugins: Plugin[];
3838
}
3939

src/plugins/images.tsx

+14-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Editor } from 'slate';
44
import { RenderBlockProps, Plugin } from 'slate-react';
55

66
interface Options {
7-
uploadFunction: (file: Blob) => Promise<string>;
7+
uploadFunction: (file: Blob) => Promise<Record<string, any>>;
88
}
99

1010
export default function images(options: Options): Plugin {
@@ -24,10 +24,13 @@ export default function images(options: Options): Plugin {
2424
);
2525
}
2626
case 'image': {
27+
const src = node.data.has('src')
28+
? node.data.get('src')
29+
: node.data.get('objectUrl');
2730
return (
2831
<ImageBlock
2932
editor={editor}
30-
src={node.data.get('src')}
33+
src={src}
3134
isFocused={isFocused}
3235
attributes={attributes}
3336
node={node}
@@ -46,12 +49,17 @@ export default function images(options: Options): Plugin {
4649
}
4750
},
4851
commands: {
49-
insertImage(editor: Editor, file: Blob) {
52+
insertImage(editor: Editor, file: Blob, data?: Record<string, any>) {
5053
/*
51-
* Inserts the ImageBlock, and runs the provided callback.
54+
* Inserts the ImageBlock, and runs the provided callback. We create a objectUrl to use if
55+
* the uploadFunction does not immediatly return a src.
5256
*/
53-
uploadFunction(file).then((src: string) =>
54-
editor.insertBlock({ data: { src }, type: 'image' })
57+
const objectUrl = URL.createObjectURL(file);
58+
uploadFunction(file).then((returnData: Record<string, any>) =>
59+
editor.insertBlock({
60+
data: { ...returnData, ...data, objectUrl },
61+
type: 'image'
62+
})
5563
);
5664
return editor;
5765
}

src/serializer.tsx

+17-4
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,20 @@ const rules: Rule[] = [
4848
};
4949
}
5050
case 'image': {
51+
const fileKey = el.getAttribute('data-file-key');
52+
const dataFromHtml = el.getAttributeNames().reduce(
53+
(data: Record<string, any>, attrName: string) => ({
54+
...data,
55+
attrName: el.getAttribute(attrName)
56+
}),
57+
{}
58+
);
5159
return {
5260
object: 'block',
5361
type: type,
5462
data: {
55-
src: el.getAttribute('src'),
56-
fileKey: el.getAttribute('data-file-key')
63+
...dataFromHtml,
64+
fileKey
5765
}
5866
};
5967
}
@@ -105,14 +113,19 @@ const rules: Rule[] = [
105113
);
106114
case 'figure':
107115
return <figure>{children}</figure>;
108-
case 'image':
116+
case 'image': {
117+
// For compatibility with https://github.com/webkom/lego
118+
const data = obj.data.toJS();
119+
const { fileKey } = data;
109120
return (
110121
<img
111122
src={obj.data.get('src')}
112-
data-file-key={obj.data.get('fileKey')}
123+
data-file-key={fileKey}
124+
{...data}
113125
alt="Placeholder"
114126
/>
115127
);
128+
}
116129
case 'image_caption':
117130
return <figcaption>{children}</figcaption>;
118131
}

0 commit comments

Comments
 (0)