Skip to content

Commit 977e705

Browse files
authored
feat: Fix for Editor.js v2.19.1 (#24)
BREAKING CHANGE: For avoiding default exports, the module export method is also changed. * Simplify demo * Avoid default exports * Fix for v2.19.1 * Fix README
1 parent e8536f1 commit 977e705

10 files changed

+94
-123
lines changed

.github/workflows/codeql-analysis.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: CodeQL
2+
on:
3+
push:
4+
branches: [master]
5+
pull_request:
6+
branches: [master]
7+
schedule:
8+
- cron: '0 3 * * 1'
9+
jobs:
10+
analyze:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
language: ['javascript']
16+
steps:
17+
- uses: actions/checkout@v2
18+
with:
19+
fetch-depth: 2
20+
- run: git checkout HEAD^2
21+
if: ${{ github.event_name == 'pull_request' }}
22+
- uses: github/codeql-action/init@v1
23+
with:
24+
languages: ${{ matrix.language }}
25+
- uses: github/codeql-action/autobuild@v1
26+
- uses: github/codeql-action/analyze@v1

README.md

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!-- markdownlint-disable first-line-h1 -->
12
<h1 align="center">Welcome to editorjs-style 👋</h1>
23
<p>
34
<a href="https://www.npmjs.com/package/editorjs-style" target="_blank">
@@ -6,12 +7,6 @@
67
<a href="#" target="_blank">
78
<img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" />
89
</a>
9-
<a href="https://lgtm.com/projects/g/hata6502/editorjs-style/alerts/">
10-
<img alt="Total alerts" src="https://img.shields.io/lgtm/alerts/g/hata6502/editorjs-style.svg?logo=lgtm&logoWidth=18"/>
11-
</a>
12-
<a href="https://lgtm.com/projects/g/hata6502/editorjs-style/context:javascript">
13-
<img alt="Language grade: JavaScript" src="https://img.shields.io/lgtm/grade/javascript/g/hata6502/editorjs-style.svg?logo=lgtm&logoWidth=18"/>
14-
</a>
1510
<a href="https://twitter.com/hata6502" target="_blank">
1611
<img alt="Twitter: hata6502" src="https://img.shields.io/twitter/follow/hata6502.svg?style=social" />
1712
</a>
@@ -24,9 +19,6 @@
2419
Editor.js may need many inline tools to styling inline elements.
2520
editorjs-style provides feature attaching any id, class and style to inline elements.
2621

27-
By using with [editorjs-inspector](https://github.com/hata6502/editorjs-inspector),
28-
Editor.js can be used like a HTML editor.
29-
3022
### [Demo](https://hata6502.github.io/editorjs-style/)
3123

3224
![Demo](https://user-images.githubusercontent.com/7702653/93618038-bd90e880-fa11-11ea-9355-f782a557b6d4.gif)
@@ -71,11 +63,7 @@ yarn test
7163

7264
## Author
7365

74-
<img alt="hata6502" src="https://avatars.githubusercontent.com/hata6502" width="48" /> **hata6502**
75-
76-
- Website: https://b-hood.site/
77-
- Twitter: [@hata6502](https://twitter.com/hata6502)
78-
- Github: [@hata6502](https://github.com/hata6502)
66+
<img alt="hata6502" src="https://avatars.githubusercontent.com/hata6502" width="48" /> **Tomoyuki Hata <[email protected]>**
7967

8068
## 🤝 Contributing
8169

@@ -89,7 +77,7 @@ Give a ⭐️ if this project helped you!
8977

9078
## 📝 License
9179

92-
Copyright © 2020 [hata6502](https://github.com/hata6502).
80+
Copyright © 2020 [Tomoyuki Hata](https://github.com/hata6502).
9381

9482
This project is [MIT](https://github.com/hata6502/editorjs-style/blob/master/LICENSE) licensed.
9583

docs/index.html

+10-20
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,37 @@
33
<head>
44
<meta charset="utf-8" />
55
<title>editorjs-style</title>
6-
<script src="https://cdn.jsdelivr.net/npm/@editorjs/editorjs@latest"></script>
7-
<script src="https://cdn.jsdelivr.net/npm/editorjs-inspector@latest"></script>
8-
<script src="https://cdn.jsdelivr.net/npm/editorjs-style@latest"></script>
9-
<script src="https://cdn.jsdelivr.net/npm/@editorjs/paragraph@latest"></script>
106
</head>
117

128
<body>
139
<div id="editorjs"></div>
1410
<button id="button" type="button">Save</button>
1511
<div id="output-data"></div>
12+
13+
<script src="https://cdn.jsdelivr.net/npm/@editorjs/editorjs@latest"></script>
14+
15+
<!-- For demo -->
16+
<script src="https://cdn.jsdelivr.net/npm/editorjs-style@latest"></script>
17+
<!-- For developing -->
18+
<!--<script src="../dist/index.js"></script>-->
19+
1620
<script>
1721
const data = {
18-
time: 1600015893556,
1922
blocks: [
2023
{
2124
type: 'paragraph',
2225
data: {
2326
text:
24-
'editorjs-style <editorjs-style style="background-color: pink;">​demo​</editorjs-style>​<editorjs-style style="display: block;\nborder: 1px solid black;">​<editorjs-style style="float: left;\nwidth: 100px;\nbackground-color: pink;\nmargin-right: 1rem;">​​​floating content​​​​</editorjs-style>​Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.​</editorjs-style>',
27+
'test <editorjs-style style="background-color: pink;">editorjs-style​​</editorjs-style> test',
2528
},
2629
},
2730
],
28-
version: '2.18.0',
2931
};
3032

31-
// To prevent breaking nested editorjs-style
32-
class ParagraphForEditorJSStyle extends Paragraph {
33-
static get enableLineBreaks() {
34-
return true;
35-
}
36-
}
37-
3833
const editorJS = new EditorJS({
3934
data,
4035
tools: {
41-
editorJSInspector: EditorJSInspector,
42-
editorJSStyle: EditorJSStyle,
43-
paragraph: {
44-
class: ParagraphForEditorJSStyle,
45-
inlineToolbar: true,
46-
},
36+
style: EditorJSStyle.StyleInlineTool,
4737
},
4838
});
4939

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"repository": "https://github.com/hata6502/editorjs-style",
1414
"license": "MIT",
15-
"author": "hata6502",
15+
"author": "Tomoyuki Hata <[email protected]>",
1616
"main": "dist/index.js",
1717
"scripts": {
1818
"fix": "prettier --write .",
@@ -23,7 +23,7 @@
2323
"webpack": "webpack"
2424
},
2525
"devDependencies": {
26-
"@editorjs/editorjs": "^2.18.0",
26+
"@editorjs/editorjs": "^2.19.1",
2727
"license-checker": "^25.0.1",
2828
"material-design-icons": "^3.0.1",
2929
"prettier": "^2.0.5",

src/EditorJSStyleElement.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
class EditorJSStyleElement extends HTMLElement {
2-
constructor() {
3-
super();
1+
class EditorJSStyleElement extends HTMLElement {}
42

5-
// To prevent Editor.js keydown event
6-
this.addEventListener('keydown', (event) => event.stopPropagation());
7-
}
8-
}
9-
10-
export default EditorJSStyleElement;
3+
export { EditorJSStyleElement };

src/EditorJSStyleError.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ class EditorJSStyleError extends Error {
1010
}
1111
}
1212

13-
export default EditorJSStyleError;
13+
export { EditorJSStyleError };

src/EditorJSStyle.ts renamed to src/StyleInlineTool.ts

+43-34
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import type {
33
InlineTool,
44
InlineToolConstructorOptions,
55
} from '@editorjs/editorjs';
6-
import EditorJSStyleElement from './EditorJSStyleElement';
7-
import EditorJSStyleError from './EditorJSStyleError';
6+
import { EditorJSStyleElement } from './EditorJSStyleElement';
7+
import { EditorJSStyleError } from './EditorJSStyleError';
88

9-
class EditorJSStyle implements InlineTool {
9+
class StyleInlineTool implements InlineTool {
1010
static get isInline() {
1111
return true;
1212
}
@@ -25,18 +25,20 @@ class EditorJSStyle implements InlineTool {
2525
return 'Style';
2626
}
2727

28+
static prepare() {
29+
if (customElements.get('editorjs-style')) {
30+
return;
31+
}
32+
33+
customElements.define('editorjs-style', EditorJSStyleElement);
34+
}
35+
2836
#actions: HTMLDivElement;
2937
#api: API;
3038

3139
constructor({ api }: InlineToolConstructorOptions) {
3240
this.#actions = document.createElement('div');
3341
this.#api = api;
34-
35-
if (customElements.get('editorjs-style')) {
36-
return;
37-
}
38-
39-
customElements.define('editorjs-style', EditorJSStyleElement);
4042
}
4143

4244
get shortcut() {
@@ -46,14 +48,16 @@ class EditorJSStyle implements InlineTool {
4648
checkState() {
4749
this.#actions.innerHTML = '';
4850

49-
const editorjsStyle = this.#api.selection.findParentTag('EDITORJS-STYLE');
51+
const editorJSStyleElement = this.#api.selection.findParentTag(
52+
'EDITORJS-STYLE'
53+
);
5054

51-
if (!editorjsStyle) {
55+
if (!editorJSStyleElement) {
5256
return false;
5357
}
5458

5559
this.#actions.innerHTML = `
56-
<div style="margin-left: 0.5rem; ">
60+
<div style="margin-bottom: 16px; margin-left: 16px; margin-right: 16px; ">
5761
<div style="display: flex; align-items: center; justify-content: space-between; ">
5862
<div>Style settings</div>
5963
@@ -72,15 +76,15 @@ class EditorJSStyle implements InlineTool {
7276
7377
<input class="id-input ${
7478
this.#api.styles.input
75-
}" placeholder="exciting" style="width: 80%; ">
79+
}" style="width: 80%; ">
7680
</label>
7781
7882
<label style="display: flex; align-items: center; justify-content: space-between; ">
7983
<span>Class</span>
8084
8185
<input class="class-input ${
8286
this.#api.styles.input
83-
}" placeholder="note editorial" style="width: 80%; ">
87+
}" style="width: 80%; ">
8488
</label>
8589
8690
<label style="display: flex; align-items: center; justify-content: space-between; ">
@@ -116,14 +120,18 @@ class EditorJSStyle implements InlineTool {
116120
}
117121

118122
deleteButton.addEventListener('click', () => {
119-
const clonedNodes = Array.from(editorjsStyle.childNodes).map((node) =>
120-
node.cloneNode(true)
121-
);
123+
const clonedNodes = Array.from(
124+
editorJSStyleElement.childNodes
125+
).map((node) => node.cloneNode(true));
122126

123127
clonedNodes.forEach((node) =>
124-
editorjsStyle.parentNode?.insertBefore(node, editorjsStyle)
128+
editorJSStyleElement.parentNode?.insertBefore(
129+
node,
130+
editorJSStyleElement
131+
)
125132
);
126-
editorjsStyle.remove();
133+
134+
editorJSStyleElement.remove();
127135

128136
if (clonedNodes.length === 0) {
129137
return;
@@ -143,31 +151,36 @@ class EditorJSStyle implements InlineTool {
143151
range.setEndAfter(clonedNodes[clonedNodes.length - 1]);
144152

145153
selection.addRange(range);
154+
this.#actions.innerHTML = '';
155+
this.#api.tooltip.hide();
146156
});
147157

148158
this.#api.tooltip.onHover(deleteButton, 'Delete style', {
149159
placement: 'top',
150160
});
151161

152-
classInput.value = editorjsStyle.className;
162+
classInput.value = editorJSStyleElement.className;
153163

154164
classInput.addEventListener('input', () =>
155-
editorjsStyle.setAttribute('class', classInput.value)
165+
editorJSStyleElement.setAttribute('class', classInput.value)
156166
);
157167

158-
idInput.value = editorjsStyle.id;
168+
idInput.value = editorJSStyleElement.id;
159169

160-
idInput.addEventListener('input', () => (editorjsStyle.id = idInput.value));
170+
idInput.addEventListener(
171+
'input',
172+
() => (editorJSStyleElement.id = idInput.value)
173+
);
161174

162-
styleTextarea.value = editorjsStyle.getAttribute('style') ?? '';
175+
styleTextarea.value = editorJSStyleElement.getAttribute('style') ?? '';
163176

164177
// To input line breaks
165178
styleTextarea.addEventListener('keydown', (event) =>
166179
event.stopPropagation()
167180
);
168181

169182
styleTextarea.addEventListener('input', () =>
170-
editorjsStyle.setAttribute('style', styleTextarea.value)
183+
editorJSStyleElement.setAttribute('style', styleTextarea.value)
171184
);
172185

173186
return true;
@@ -198,17 +211,13 @@ class EditorJSStyle implements InlineTool {
198211
}
199212

200213
surround(range: Range) {
201-
const editorjsStyle = new EditorJSStyleElement();
214+
const editorjsStyleElement = new EditorJSStyleElement();
202215

203-
editorjsStyle.append(
204-
range.collapsed ? 'new style' : range.extractContents()
205-
);
216+
editorjsStyleElement.append(range.extractContents());
206217

207-
setTimeout(() => {
208-
range.insertNode(editorjsStyle);
209-
this.#api.selection.expandToTag(editorjsStyle);
210-
});
218+
range.insertNode(editorjsStyleElement);
219+
this.#api.selection.expandToTag(editorjsStyleElement);
211220
}
212221
}
213222

214-
export default EditorJSStyle;
223+
export { StyleInlineTool };

src/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
import EditorJSStyle from './EditorJSStyle';
2-
3-
export default EditorJSStyle;
1+
export { StyleInlineTool } from './StyleInlineTool';

webpack.config.js

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ module.exports = {
1414
output: {
1515
filename: 'index.js',
1616
library: 'EditorJSStyle',
17-
libraryExport: 'default',
1817
libraryTarget: 'umd',
1918
},
2019
};

0 commit comments

Comments
 (0)