Skip to content
This repository was archived by the owner on Nov 13, 2021. It is now read-only.

Commit 57ff44e

Browse files
committed
WIP: import md feature
1 parent 086a434 commit 57ff44e

File tree

6 files changed

+175
-127
lines changed

6 files changed

+175
-127
lines changed

dist/bundle.js

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

src/data.js

Lines changed: 85 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,99 @@
11
export const dataField = {
2-
blocks: [
3-
{
4-
type: 'header',
5-
data: {
6-
text: 'Editor.js',
7-
level: 2,
8-
},
2+
blocks: [
3+
{
4+
type: 'header',
5+
data: {
6+
text: 'Editor.js',
7+
level: 2,
98
},
10-
{
11-
type: 'paragraph',
12-
data: {
13-
text: 'Hey. Meet the new Editor. On this page you can see it in action — try to edit this text. Source code of the page contains the example of connection and configuration.',
14-
},
9+
},
10+
{
11+
type: 'paragraph',
12+
data: {
13+
text: 'Hey. Meet the new Editor. On this page you can see it in action — try to edit this text. Source code of the page contains the example of connection and configuration.',
1514
},
16-
{
17-
type: 'header',
18-
data: {
19-
text: 'Key features',
20-
level: 3,
21-
},
15+
},
16+
{
17+
type: 'header',
18+
data: {
19+
text: 'Key features',
20+
level: 3,
2221
},
23-
{
24-
type: 'list',
25-
data: {
26-
items: [
27-
'It is a block-styled editor',
28-
'It returns clean data output in JSON',
29-
'Designed to be extendable and pluggable with a simple API',
30-
],
31-
style: 'unordered',
32-
},
22+
},
23+
{
24+
type: 'list',
25+
data: {
26+
items: [
27+
'It is a block-styled editor',
28+
'It returns clean data output in JSON',
29+
'Designed to be extendable and pluggable with a simple API',
30+
],
31+
style: 'unordered',
3332
},
34-
{
35-
type: 'header',
36-
data: {
37-
text: 'What does it mean «block-styled editor»',
38-
level: 3,
39-
},
33+
},
34+
{
35+
type: 'header',
36+
data: {
37+
text: 'What does it mean «block-styled editor»',
38+
level: 3,
4039
},
41-
{
42-
type: 'paragraph',
43-
data: {
44-
text: 'Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js <mark class=\"cdx-marker\">workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc</mark>. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor\'s Core.',
45-
},
40+
},
41+
{
42+
type: 'paragraph',
43+
data: {
44+
text: 'Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js <mark class=\"cdx-marker\">workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc</mark>. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor\'s Core.',
4645
},
47-
{
48-
type: 'paragraph',
49-
data: {
50-
text: 'There are dozens of <a href="https://github.com/editor-js">ready-to-use Blocks</a> and the <a href="https://editorjs.io/creating-a-block-tool">simple API</a> for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games.',
51-
},
46+
},
47+
{
48+
type: 'paragraph',
49+
data: {
50+
text: 'There are dozens of <a href="https://github.com/editor-js">ready-to-use Blocks</a> and the <a href="https://editorjs.io/creating-a-block-tool">simple API</a> for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games.',
5251
},
53-
{
54-
type: 'header',
55-
data: {
56-
text: 'What does it mean clean data output',
57-
level: 3,
58-
},
52+
},
53+
{
54+
type: 'header',
55+
data: {
56+
text: 'What does it mean clean data output',
57+
level: 3,
5958
},
60-
{
61-
type: 'paragraph',
62-
data: {
63-
text: 'Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below',
64-
},
59+
},
60+
{
61+
type: 'paragraph',
62+
data: {
63+
text: 'Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below',
6564
},
66-
{
67-
type: 'paragraph',
68-
data: {
69-
text: 'Given data can be used as you want: render with HTML for <code class="inline-code">Web clients</code>, render natively for <code class="inline-code">mobile apps</code>, create markup for <code class="inline-code">Facebook Instant Articles</code> or <code class="inline-code">Google AMP</code>, generate an <code class="inline-code">audio version</code> and so on.',
70-
},
65+
},
66+
{
67+
type: 'paragraph',
68+
data: {
69+
text: 'Given data can be used as you want: render with HTML for <code class="inline-code">Web clients</code>, render natively for <code class="inline-code">mobile apps</code>, create markup for <code class="inline-code">Facebook Instant Articles</code> or <code class="inline-code">Google AMP</code>, generate an <code class="inline-code">audio version</code> and so on.',
7170
},
72-
{
73-
type: 'paragraph',
74-
data: {
75-
text: 'Clean data is useful to sanitize, validate and process on the backend.',
76-
},
71+
},
72+
{
73+
type: 'paragraph',
74+
data: {
75+
text: 'Clean data is useful to sanitize, validate and process on the backend.',
7776
},
78-
{
79-
type: 'delimiter',
80-
data: {},
77+
},
78+
{
79+
type: 'delimiter',
80+
data: {},
81+
},
82+
{
83+
type: 'paragraph',
84+
data: {
85+
text: 'We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make its core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏',
8186
},
82-
{
83-
type: 'paragraph',
84-
data: {
85-
text: 'We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make its core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏',
86-
},
87+
},
88+
{
89+
type: 'image',
90+
data: {
91+
url: 'assets/codex2x.png',
92+
caption: '',
93+
stretched: false,
94+
withBorder: true,
95+
withBackground: false,
8796
},
88-
{
89-
type: 'image',
90-
data: {
91-
url: 'assets/codex2x.png',
92-
caption: '',
93-
stretched: false,
94-
withBorder: true,
95-
withBackground: false,
96-
},
97-
},
98-
],
99-
};
97+
},
98+
],
99+
};

src/file-handler.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,19 @@ export function fileDownloadHandler(content, fileName) {
1212
}
1313

1414
export function fileUploadHandler(event) {
15-
// console.log(event.target);
15+
console.log('it works somehow');
1616
const remark = require('remark');
1717
const file = event.target.files[0];
1818
const reader = new FileReader();
19-
const test = reader.onload = (e) => {
19+
reader.onload = (e) => {
2020
console.log(remark().parse(e.target.result));
2121
return remark().parse(e.target.result);
2222
return e.target.result;
23-
2423
};
2524

2625
return reader.readAsText(file);
2726
}
2827

29-
3028
export function parseFromMarkdown(string) {
3129
const remark = require('remark');
3230

src/import-markdown.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { fileUploadHandler } from './file-handler';
2+
export default class ImportMarkdown {
3+
constructor({ data, api }) {
4+
this.data = data;
5+
this.api = api;
6+
}
7+
8+
static get toolbox() {
9+
return {
10+
title: 'Import Markdown',
11+
icon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="rgb(112, 118, 132)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-fileUpload"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="17 8 12 3 7 8"></polyline><line x1="12" y1="3" x2="12" y2="15"></line></svg>',
12+
};
13+
}
14+
15+
render() {
16+
const doc = document.createElement('input');
17+
doc.setAttribute('id', 'file-upload');
18+
doc.setAttribute('type', 'file');
19+
doc.setAttribute('style', 'display: none');
20+
doc.setAttribute('name', 'files[]');
21+
doc.setAttribute('onload', this.parseMarkdown());
22+
23+
// TODO: return import method
24+
25+
return doc;
26+
}
27+
28+
async parseMarkdown() {
29+
30+
const a = {};
31+
const data = await this.api.saver.save();
32+
a.content = data.blocks;
33+
34+
const fileUpload = document.getElementById('file-upload');
35+
36+
fileUpload.onchange = (e) => {
37+
const file = e.target.files[0];
38+
console.log(file);
39+
40+
const reader = new FileReader();
41+
reader.readAsText(file, 'UTF-8');
42+
43+
reader.onload = (readerEvent) => {
44+
const content = readerEvent.target.result;
45+
console.log(content);
46+
};
47+
};
48+
fileUpload.click();
49+
console.log('fileupload log');
50+
console.log(fileUpload);
51+
}
52+
53+
save() {
54+
return {
55+
message: 'Uploading Markdown',
56+
};
57+
}
58+
}

src/index.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,11 @@ import LinkTool from '@editorjs/link';
1313
import Table from '@editorjs/table';
1414
import Marker from '@editorjs/marker';
1515
import MarkdownParser from './markdown-parser';
16+
import ImportMarkdown from './import-markdown';
1617
import { fileUploadHandler, parseFromMarkdown } from './file-handler';
1718

1819
import { dataField } from './data';
1920

20-
const uploadButton = document.getElementById('fileUpload');
21-
const fileToParse= uploadButton.addEventListener('change', (e) => fileUploadHandler(e));
22-
23-
const importButton = document.getElementById('parseButton');
24-
25-
importButton.onclick
26-
27-
2821
const saveButton = document.getElementById('saveButton');
2922
const newDataField = {};
3023
const editor = new EditorJS({
@@ -110,6 +103,8 @@ const editor = new EditorJS({
110103

111104
markdownParser: MarkdownParser,
112105

106+
markdownImporter: ImportMarkdown,
107+
113108
},
114109

115110
/**
@@ -125,7 +120,6 @@ const editor = new EditorJS({
125120
saveButton.click();
126121
},
127122
onChange() {
128-
console.log(test);
129123
console.log('something changed');
130124
},
131125
});

src/markdown-parser.js

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,28 @@ export default class MarkdownParser {
1818

1919
static get toolbox() {
2020
return {
21-
title: 'Markdown Parser',
22-
icon: '<svg xmlns="http://www.w3.org/2000/svg" width="17" height="15" viewBox="0 0 24 24" fill="none" stroke="#707684" stroke-width="0.5" stroke-linecap="round" stroke-linejoin="round" class="feather feather-save"><path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"></path><polyline points="17 21 17 13 7 13 7 21"></polyline><polyline points="7 3 7 8 15 8"></polyline></svg>',
21+
title: 'Download Markdown',
22+
icon: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="rgb(112, 118, 132)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-download"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="7 10 12 15 17 10"></polyline><line x1="12" y1="15" x2="12" y2="3"></line></svg>',
2323
};
2424
}
2525

2626
render() {
27-
const buttons = document.createElement('div');
27+
const doc = document.createElement('div');
2828

29-
const exportButton = document.createElement('button');
30-
exportButton.setAttribute('class', 'MarkdownButton');
31-
exportButton.append('Export To Markdown');
32-
exportButton.setAttribute('onclick', this.getContent());
33-
34-
//const importButton = document.createElement('file');
35-
//importButton.setAttribute('class', 'MarkdownButton');
36-
//importButton.append('Import To Markdown');
37-
38-
// const importButton = document.createElement('form');
39-
// const fileUpload = document.createElement('input');
40-
// fileUpload.setAttribute('class', 'MarkdownButton');
41-
// fileUpload.setAttribute('id', 'exportUpload');
42-
// fileUpload.setAttribute('type', 'file');
43-
//fileUpload.setAttribute('onchange', fileUploadHandler(event));
44-
// importButton.append(fileUpload);
45-
buttons.append(exportButton);
46-
// buttons.append(importButton);
47-
return buttons;
29+
this.getContent();
30+
return doc;
4831
}
4932

5033
async getContent() {
34+
const today = new Date();
35+
const dd = String(today.getDate()).padStart(2, '0');
36+
const mm = String(today.getMonth() + 1).padStart(2, '0'); // January is 0!
37+
const yyyy = today.getFullYear();
38+
5139
const a = {};
5240
const data = await this.api.saver.save();
5341

5442
a.content = data.blocks;
55-
console.log(a.content);
5643

5744
// TODO: remove html tags in paragraphs (code, highlight, a href)
5845
const b = a.content.map((item) => {
@@ -78,13 +65,12 @@ export default class MarkdownParser {
7865
}
7966
});
8067

81-
fileDownloadHandler(b.join('\n'), 'example.md');
82-
console.log(b);
68+
fileDownloadHandler(b.join('\n'), `entry_${dd}-${mm}-${yyyy}.md`);
8369
}
8470

8571
save() {
8672
return {
87-
message: 'Parsing Markdown',
73+
message: 'Downloading Markdown',
8874
};
8975
}
9076
}

0 commit comments

Comments
 (0)