Skip to content

Commit ccfdf9d

Browse files
Merge pull request #27 from dimitrov-adrian/improve-change-handler
1.3.0
2 parents 9e83842 + 4453386 commit ccfdf9d

16 files changed

+694
-526
lines changed

package-lock.json

Lines changed: 28 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "directus-extension-editorjs",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"author": {
55
"email": "dimitrov.adrian+gh@gmail.com",
66
"name": "Adrian Dimitrov"
@@ -47,19 +47,21 @@
4747
"@editorjs/image": "^2.6.2",
4848
"@editorjs/inline-code": "^1.3.1",
4949
"@editorjs/marker": "^1.2.2",
50+
"@editorjs/nested-list": "^1.0.2",
5051
"@editorjs/paragraph": "^2.8.0",
51-
"editorjs-text-alignment-blocktune": "^1.0.3",
5252
"@editorjs/personality": "^2.0.2",
5353
"@editorjs/quote": "^2.4.0",
5454
"@editorjs/raw": "^2.3.0",
5555
"@editorjs/simple-image": "^1.4.1",
5656
"@editorjs/table": "^2.0.1",
5757
"@editorjs/underline": "^1.0.0",
5858
"@editorjs/warning": "^1.2.0",
59+
"editorjs-undo": "^1.0.0",
5960
"@itech-indrustries/editorjs-strikethrough": "^1.0.0",
6061
"directus-codestyle": "dimitrov-adrian/directus-codestyle",
6162
"editorjs-alert": "^1.0.3",
6263
"editorjs-list": "^2.1.4",
64+
"editorjs-text-alignment-blocktune": "^1.0.3",
6365
"lodash": "^4.17.21",
6466
"typescript": "^4.5.2"
6567
},

src/custom-plugins/editorjs-uploader.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* Modified version of https://github.com/editor-js/image/blob/master/src/uploader.js
33
*/
44
export default class Uploader {
5-
constructor({ config, onUpload, onError }) {
5+
constructor({ config, getCurrentFile, onUpload, onError }) {
6+
this.getCurrentFile = getCurrentFile;
67
this.config = config;
78
this.onUpload = onUpload;
89
this.onError = onError;
@@ -40,31 +41,41 @@ export default class Uploader {
4041
}
4142

4243
uploadSelectedFile({ onPreview }) {
43-
this.config.uploader.setFileHandler((file) => {
44-
if (file) {
45-
const response = {
46-
success: 1,
47-
file: {
48-
width: file.width,
49-
height: file.height,
50-
size: file.filesize,
51-
name: file.filename_download,
52-
title: file.title,
53-
extension: file.filename_download.split('.').pop(),
54-
fileId: file.id,
55-
fileURL: this.config.uploader.baseURL + 'files/' + file.id,
56-
url: this.config.uploader.baseURL + 'assets/' + file.id,
57-
},
58-
};
44+
if (this.getCurrentFile) {
45+
const currentPreview = this.getCurrentFile();
46+
if (currentPreview) {
47+
this.config.uploader.setCurrentPreview(
48+
this.config.uploader.addTokenToURL(currentPreview) + '&key=system-large-contain'
49+
);
50+
}
51+
}
5952

60-
onPreview(this.config.uploader.addTokenToURL(response.file.fileURL));
61-
this.onUpload(response);
62-
} else {
53+
this.config.uploader.setFileHandler((file) => {
54+
if (!file) {
6355
this.onError({
6456
success: 0,
6557
message: this.config.t.no_file_selected,
6658
});
59+
return;
6760
}
61+
62+
const response = {
63+
success: 1,
64+
file: {
65+
width: file.width,
66+
height: file.height,
67+
size: file.filesize,
68+
name: file.filename_download,
69+
title: file.title,
70+
extension: file.filename_download.split('.').pop(),
71+
fileId: file.id,
72+
fileURL: this.config.uploader.baseURL + 'files/' + file.id,
73+
url: this.config.uploader.baseURL + 'assets/' + file.id,
74+
},
75+
};
76+
77+
onPreview(this.config.uploader.addTokenToURL(response.file.fileURL));
78+
this.onUpload(response);
6879
});
6980
}
7081
}

src/custom-plugins/plugin-attaches-patch.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,23 @@ import Uploader from './editorjs-uploader.js';
66
* https://github.com/editor-js/attaches/blob/master/src/index.js
77
*/
88
export default class extends AttachesTool {
9-
static get isReadOnlySupported() {
10-
return true;
11-
}
9+
constructor(params) {
10+
super(params);
1211

13-
constructor({ data, config, api, readOnly }) {
14-
super({ data, config, api });
15-
this.readOnly = !!readOnly;
12+
this.config.uploader = params.config.uploader;
1613
this.uploader = new Uploader({
1714
config: this.config,
1815
onUpload: (response) => this.onUpload(response),
1916
onError: (error) => this.uploadingFailed(error),
2017
});
21-
}
2218

23-
prepareUploadButton() {
24-
if (!this.readOnly) {
25-
super.prepareUploadButton();
26-
}
19+
// Until get https://github.com/editor-js/attaches/issues/50 solved, this is required.
20+
this.onUpload = (response) => {
21+
super.onUpload(response);
22+
params.block.save().then((state) => {
23+
params.api.blocks.update(state.id, state.data);
24+
});
25+
};
2726
}
2827

2928
showFileData() {
@@ -34,8 +33,5 @@ export default class extends AttachesTool {
3433
downloadButton.href = this.uploader.config.uploader.addTokenToURL(this.data.file.url);
3534
}
3635
}
37-
if (this.readOnly && this.nodes.title) {
38-
this.nodes.title.contentEditable = false;
39-
}
4036
}
4137
}

src/custom-plugins/plugin-image-patch.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import Uploader from './editorjs-uploader.js';
66
* https://github.com/editor-js/image/blob/master/src/index.js
77
*/
88
export default class extends ImageTool {
9-
constructor({ data, config, api, readOnly }) {
10-
super({ data, config, api, readOnly });
9+
constructor(params) {
10+
super(params);
1111

1212
this.uploader = new Uploader({
1313
config: this.config,
14+
getCurrentFile: () => this.data?.file?.url,
1415
onUpload: (response) => this.onUpload(response),
1516
onError: (error) => this.uploadingFailed(error),
1617
});

src/custom-plugins/plugin-list-patch.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/custom-plugins/plugin-personality-patch.js

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,28 @@ import Uploader from './editorjs-uploader.js';
55
* Patch allows custom uploader.
66
* https://github.com/editor-js/personality/blob/master/src/index.js
77
*/
8+
9+
const LOADER_DELAY = 500;
10+
811
export default class extends Personality {
9-
constructor({ data, config, api, readOnly }) {
10-
super({ data, config, api });
11-
this.readOnly = !!readOnly;
12-
this.config.uploader = config.uploader;
12+
constructor(params) {
13+
super(params);
14+
15+
this.config.uploader = params.config.uploader;
1316
this.uploader = new Uploader({
1417
config: this.config,
18+
getCurrentFile: () => this.data.photo,
1519
onUpload: (response) => this.onUpload({ body: response }),
1620
onError: (error) => this.uploadingFailed(error),
1721
});
22+
23+
// Until get https://github.com/editor-js/attaches/issues/50 solved, this is required.
24+
this.onUpload = (response) => {
25+
super.onUpload(response);
26+
params.block.save().then((state) => {
27+
params.api.blocks.update(state.id, state.data);
28+
});
29+
};
1830
}
1931

2032
setFullImageSource(image) {
@@ -26,34 +38,15 @@ export default class extends Personality {
2638
setTimeout(() => {
2739
this.nodes.photo.classList.remove(this.CSS.loader);
2840
this.setFullImageSource(this.data.photo);
29-
}, 500);
30-
}
31-
32-
static get isReadOnlySupported() {
33-
return true;
41+
}, LOADER_DELAY);
3442
}
3543

3644
render() {
3745
const result = super.render();
38-
39-
// Clear events.
40-
if (this.readOnly) {
41-
this.nodes.photo.replaceWith(this.nodes.photo.cloneNode(true));
42-
}
43-
4446
if (this.data.photo) {
4547
this.setFullImageSource(this.data.photo);
4648
}
4749

4850
return result;
4951
}
50-
51-
make(...args) {
52-
const result = super.make(...args);
53-
if (this.readOnly && result.contentEditable) {
54-
result.contentEditable = false;
55-
}
56-
57-
return result;
58-
}
5952
}

0 commit comments

Comments
 (0)