|
@@ -2,55 +2,267 @@ const contentDiv = document.getElementById('editor_block');
|
|
|
const titleButton = document.getElementById('title_button');
|
|
|
const submitButton = document.getElementById('submit_button');
|
|
|
const contentApiUrl = `${PORTAL_SERVER}contents?url=${(JSON.parse(document.getElementById('url').textContent)).url}`
|
|
|
+
|
|
|
+aa = "";
|
|
|
+frontMatters = [];
|
|
|
+editorBlocks = [];
|
|
|
+//postData = { content: "", url: "" };
|
|
|
+var editor;
|
|
|
axios.get(contentApiUrl).then(({ data }) => {
|
|
|
+ //alert(JSON.stringify(data[0]['content']));
|
|
|
+ aa = data[0]['content'];
|
|
|
const content = _.get(data, '0.content', '');
|
|
|
- var blockArray = [{title: '', data: []}];
|
|
|
+ var blockArray = [{ title: '', data: [] }];
|
|
|
var blockCount = 0;
|
|
|
- const {frontMatters, preBlockArray} = parseMd(content);
|
|
|
- for (var blockData of preBlockArray) {
|
|
|
- blockCount = loadDataToBlock(blockArray, blockCount, blockData);
|
|
|
- }
|
|
|
- titleButton.onclick = function() {
|
|
|
- blockCount = loadDataToBlock(blockArray, blockCount);
|
|
|
+ blocks = parseMd(content);
|
|
|
+ editor_block = document.getElementById('editor_block');
|
|
|
+ //alert(blocks[0]['text']);
|
|
|
+
|
|
|
+ ul = document.createElement('ul');
|
|
|
+ ul.id = "sortable";
|
|
|
+ for (i = 0; i < blocks.length; i++) {
|
|
|
+ li = document.createElement('li');
|
|
|
+ odiv = document.createElement('div');
|
|
|
+ odiv.style.border = 'inset 1px gray';
|
|
|
+ if (blocks[i]['type'] == "para") {
|
|
|
+ editorBlocks.push({ type: "paragraph", data: { text: blocks[i]['text'] } });
|
|
|
+ tmp = document.createElement('textarea');
|
|
|
+ tmp.style.border = 'outset 5px pink';
|
|
|
+ tmp.style.width = '100%';
|
|
|
+ tmp.value = blocks[i]['text'];
|
|
|
+ odiv.appendChild(tmp);
|
|
|
+ }
|
|
|
+ else if (blocks[i]['type'] == "br") {
|
|
|
+ editorBlocks.push({ type: "paragraph", data: { text: "" } });
|
|
|
+ tmp = document.createElement('br');
|
|
|
+ odiv.appendChild(tmp);
|
|
|
+ }
|
|
|
+ else if (blocks[i]['type'] == "img") {
|
|
|
+ ampimg = blocks[i]['text'];
|
|
|
+ //alert(ampimg.indexOf("width=\"",84));
|
|
|
+ //alert(ampimg.substr(ampimg.indexOf("alt=\"") + 5, ampimg.indexOf("\"", ampimg.indexOf("alt=\"") + 5) - ampimg.indexOf("alt=\"") - 5));
|
|
|
+ tmpsrc = BHOUSE_SERVER + JSON.parse(document.getElementById('url').textContent).url + '/' + ampimg.substr(ampimg.indexOf("src=\"") + 5, ampimg.indexOf(".webp") - ampimg.indexOf("src=\""));
|
|
|
+ //tmpsrc = ampimg.substr(ampimg.indexOf("src=\"") + 5, ampimg.indexOf(".webp") - ampimg.indexOf("src=\""));
|
|
|
+ editorBlocks.push({
|
|
|
+ type: "image", data: {
|
|
|
+ file: {
|
|
|
+ url: tmpsrc,
|
|
|
+ width: parseInt(ampimg.substr(ampimg.indexOf("width=\"") + 7, ampimg.indexOf("\"", ampimg.indexOf("width=\"") + 7) - ampimg.indexOf("width=\"") - 7)),
|
|
|
+ height: parseInt(ampimg.substr(ampimg.indexOf("height=\"") + 8, ampimg.indexOf("\"", ampimg.indexOf("height=\"") + 8) - ampimg.indexOf("height=\"") - 8)),
|
|
|
+ },
|
|
|
+ caption: ampimg.substr(ampimg.indexOf("alt=\"") + 5, ampimg.indexOf("\"", ampimg.indexOf("alt=\"") + 5) - ampimg.indexOf("alt=\"") - 5).toString(),
|
|
|
+ stretched: false,
|
|
|
+ withBorder: true,
|
|
|
+ withBackground: false,
|
|
|
+ }
|
|
|
+ });
|
|
|
+ //alert(ampimg.substr(ampimg.indexOf("height=\"") + 8, ampimg.indexOf("\"", ampimg.indexOf("height=\"") + 8) - ampimg.indexOf("height=\"") - 8));
|
|
|
+ img = document.createElement('img');
|
|
|
+ img.style.width = '100%';
|
|
|
+ img.src = tmpsrc;
|
|
|
+ odiv.appendChild(img);
|
|
|
+ }
|
|
|
+ else if (blocks[i]['type'] == "title") {
|
|
|
+ blocks[i]['text'] = blocks[i]['text'].replace("### **", "").replace("**", "")
|
|
|
+ editorBlocks.push({ type: "header", data: { text: blocks[i]['text'] } });
|
|
|
+ tmp = document.createElement('h3');
|
|
|
+ tmp.innerHTML = blocks[i]['text'];
|
|
|
+ odiv.appendChild(tmp);
|
|
|
+ }
|
|
|
+ else if (blocks[i]['type'] == "mt5") {
|
|
|
+ //alert('yo');
|
|
|
+ editorBlocks.push({ type: "delimiter", data: {} });
|
|
|
+ }
|
|
|
+ li.appendChild(odiv);
|
|
|
+ ul.appendChild(li);
|
|
|
+
|
|
|
}
|
|
|
+ editor_block.appendChild(ul);
|
|
|
+ $("#sortable").sortable();
|
|
|
+ $("#sortable").disableSelection();
|
|
|
+
|
|
|
+ $("#editor_block").css({ "display": "none" });
|
|
|
+
|
|
|
+ editor = new EditorJS({
|
|
|
+ readOnly: false,
|
|
|
+ holder: 'editorjs',
|
|
|
+
|
|
|
+ tools: {
|
|
|
+ header: {
|
|
|
+ class: Header,
|
|
|
+ config: {
|
|
|
+ placeholder: 'Header'
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ image: {
|
|
|
+ class: ImageTool,
|
|
|
+ config: {
|
|
|
+ endpoints: {
|
|
|
+ byFile: '/backstage/upload', // Your backend file uploader endpoint
|
|
|
+ byUrl: '/backstage/getimage', // Your endpoint that provides uploading by Url
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ,
|
|
|
+ delimiter: Delimiter
|
|
|
+ }
|
|
|
+ ,
|
|
|
+ data: { blocks: editorBlocks }
|
|
|
+ ,
|
|
|
+ /*
|
|
|
+ data: {
|
|
|
+ blocks: [
|
|
|
+ {
|
|
|
+ type: "header",
|
|
|
+ data: {
|
|
|
+ text: "Editor.js",
|
|
|
+ level: 2
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ type: "header",
|
|
|
+ data: {
|
|
|
+ text: "Key features",
|
|
|
+ level: 3
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ type: "header",
|
|
|
+ data: {
|
|
|
+ text: "What does it mean «block-styled editor»",
|
|
|
+ level: 3
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ type: 'paragraph',
|
|
|
+ data: {
|
|
|
+ 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.`
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ type: 'delimiter',
|
|
|
+ data: {}
|
|
|
+ },
|
|
|
+ {
|
|
|
+ type: 'image',
|
|
|
+ data: {
|
|
|
+ url: 'http://localhost:1313/img/logo2.png',
|
|
|
+ caption: '',
|
|
|
+ stretched: false,
|
|
|
+ withBorder: true,
|
|
|
+ withBackground: false,
|
|
|
+ }
|
|
|
+ },
|
|
|
+ ]
|
|
|
+ },*/
|
|
|
+
|
|
|
+ onReady: function () {
|
|
|
+ //saveButton.click();
|
|
|
+ },
|
|
|
+ onChange: function (api, block) {
|
|
|
+ console.log('something changed', block);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ /* for (var blockData of preBlockArray) {
|
|
|
+ blockCount = loadDataToBlock(blockArray, blockCount, blockData);
|
|
|
+ }
|
|
|
+ titleButton.onclick = function () {
|
|
|
+ blockCount = loadDataToBlock(blockArray, blockCount);
|
|
|
+ } */
|
|
|
+});
|
|
|
+
|
|
|
+submitButton.onclick = function () {
|
|
|
+
|
|
|
+ editor.save().then((outputData) => {
|
|
|
+ console.log('Article data: ', outputData);
|
|
|
|
|
|
- submitButton.onclick = function() {
|
|
|
var mdContent = '';
|
|
|
for (var frontMatter of frontMatters) {
|
|
|
mdContent += frontMatter + '\n';
|
|
|
}
|
|
|
- for (var idx=0; idx<blockArray.length; idx++) {
|
|
|
- if (_.get(blockArray[idx], 'title', '').includes('敘述')) {
|
|
|
- mdContent += `\n<!-- ### **${_.get(blockArray[idx], 'title', '')}**-->\n`
|
|
|
- } else {
|
|
|
- mdContent += `\n### **${_.get(blockArray[idx], 'title', '')}**\n`
|
|
|
+
|
|
|
+ for (i = 0; i < outputData.blocks.length; i++) {
|
|
|
+ //alert(block.type);
|
|
|
+ block = outputData.blocks[i];
|
|
|
+ if (block.type == "header") {
|
|
|
+ mdContent += '\n### **' + block.data.text + '**\n';
|
|
|
+ }
|
|
|
+ else if (block.type == "paragraph") {
|
|
|
+ mdContent += '\n' + block.data.text + '\n';
|
|
|
+ }
|
|
|
+ else if (block.type == "image") {
|
|
|
+ mdContent += '\n<amp-img\n alt="' + block.data.caption
|
|
|
+ + '"\n src="' + block.data.file.url.replace(BHOUSE_SERVER + JSON.parse(document.getElementById('url').textContent).url + '/', '')
|
|
|
+ + '"\n height="' + block.data.file.height
|
|
|
+ + '"\n width="' + block.data.file.width
|
|
|
+ + '"\n layout="responsive">\n</amp-img>\n';
|
|
|
}
|
|
|
- for (var data of _.get(blockArray[idx], 'data', [])) {
|
|
|
- if (_.get(_.keys(data), 0) === 'description') {
|
|
|
- if (_.get(data, 'description.text', '').includes('\n')) {
|
|
|
- for (const line of _.get(data, 'description.text', '').split('\n')) {
|
|
|
- mdContent += `\n${line} `;
|
|
|
- }
|
|
|
- } else {
|
|
|
- mdContent += `\n${_.get(data, 'description.text', '')}`;
|
|
|
+ else if (block.type == "delimiter") {
|
|
|
+ mdContent += '\n{{% chuz-div class="mt-5" %}}\n';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //alert(mdContent);
|
|
|
+
|
|
|
+ postData = {
|
|
|
+ content: mdContent,
|
|
|
+ url: (JSON.parse(document.getElementById('url').textContent)).url
|
|
|
+ };
|
|
|
+ axios.post(contentApiUrl, json = postData);
|
|
|
+
|
|
|
+ }).catch((error) => {
|
|
|
+ console.log('Saving failed: ', error)
|
|
|
+ });
|
|
|
+
|
|
|
+ var mdContent = '';
|
|
|
+ for (var frontMatter of frontMatters) {
|
|
|
+ mdContent += frontMatter + '\n';
|
|
|
+ }
|
|
|
+
|
|
|
+ //alert(bb);
|
|
|
+ /* for(var eBlock in outputData.blocks)
|
|
|
+ {
|
|
|
+ alert(eBlock.type);
|
|
|
+ } */
|
|
|
+
|
|
|
+ for (var idx = 0; idx < blockArray.length; idx++) {
|
|
|
+ if (_.get(blockArray[idx], 'title', '').includes('敘述')) {
|
|
|
+ mdContent += `\n<!-- ### **${_.get(blockArray[idx], 'title', '')}**-->\n`
|
|
|
+ } else {
|
|
|
+ mdContent += `\n### **${_.get(blockArray[idx], 'title', '')}**\n`
|
|
|
+ }
|
|
|
+ for (var data of _.get(blockArray[idx], 'data', [])) {
|
|
|
+ if (_.get(_.keys(data), 0) === 'description') {
|
|
|
+ if (_.get(data, 'description.text', '').includes('\n')) {
|
|
|
+ for (const line of _.get(data, 'description.text', '').split('\n')) {
|
|
|
+ mdContent += `\n${line} `;
|
|
|
}
|
|
|
- } else if (_.get(_.keys(data), 0) === 'image') {
|
|
|
- ampImgForm = `\n<amp-img\
|
|
|
+ } else {
|
|
|
+ mdContent += `\n${_.get(data, 'description.text', '')}`;
|
|
|
+ }
|
|
|
+ } else if (_.get(_.keys(data), 0) === 'image') {
|
|
|
+ ampImgForm = `\n<amp-img\
|
|
|
\n alt="${_.get(data, 'image.alt', '小寶優居')}"\
|
|
|
\n src="${_.get(data, 'image.src', '')}"\
|
|
|
\n height="${_.get(data, 'image.height', 300)}"\
|
|
|
\n width="${_.get(data, 'image.width', 400)}"\
|
|
|
\n layout="${_.get(data, 'image.layout', 'responsive')}">\
|
|
|
\n</amp-img>\n`;
|
|
|
- mdContent += ampImgForm;
|
|
|
- }
|
|
|
+ mdContent += ampImgForm;
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- const postData = {
|
|
|
- content: mdContent,
|
|
|
- url: (JSON.parse(document.getElementById('url').textContent)).url
|
|
|
- }
|
|
|
- axios.post(contentApiUrl, json=postData);
|
|
|
+ /* const postData = {
|
|
|
+ content: mdContent,
|
|
|
+ url: (JSON.parse(document.getElementById('url').textContent)).url
|
|
|
+ }; */
|
|
|
+
|
|
|
+ if (aa != postData['content']) {
|
|
|
+ //alert(postData['content']);
|
|
|
+ alert(mdContent);
|
|
|
}
|
|
|
-});
|
|
|
+ //axios.post(contentApiUrl, json = postData);
|
|
|
+
|
|
|
+}
|