From b3751c0ba506e12ffef2a74923a5750e86e0f4c5 Mon Sep 17 00:00:00 2001 From: mansi Date: Fri, 29 Sep 2023 17:47:07 +0530 Subject: [PATCH] feat : autosave functionality in editor --- .../api/post/content-types/post/lifecycles.js | 15 ++++++++--- admin/src/api/post/controllers/post.js | 22 +++++++++++++++ admin/src/api/post/routes/custom.js | 8 ++++++ .../admin/src/components/editorjs/index.js | 27 +++++++++++++++++++ 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/admin/src/api/post/content-types/post/lifecycles.js b/admin/src/api/post/content-types/post/lifecycles.js index 602f0c8e..7b2c8a8e 100644 --- a/admin/src/api/post/content-types/post/lifecycles.js +++ b/admin/src/api/post/content-types/post/lifecycles.js @@ -76,6 +76,9 @@ async function modifyContentAndSetErrorMsg(event) { // generate table of contents await generateTOC(result, event); await generateNewToc(result, event); + if (result.tags) { + event.params.data.tags = await TagsInput(result.tags); + } } } @@ -180,10 +183,12 @@ async function generateTOC(result, event) { event.params.data.content = dom.serialize(); event.params.data.toc = toc += ""; - event.params.data.tags = await TagsInput(result.tags); // set published on - if (!event.params.data.published_on) { + if ( + event.params.data.publishedAt == null && + !event.params.data.published_on + ) { event.params.data.published_on = new Date(); } } @@ -196,10 +201,12 @@ async function generateNewToc(result, event) { event.params.data.new_content = dom.serialize(); event.params.data.new_toc = toc += ""; - event.params.data.tags = await TagsInput(result.tags); // set published on - if (!event.params.data.published_on) { + if ( + event.params.data.publishedAt == null && + !event.params.data.published_on + ) { event.params.data.published_on = new Date(); } } diff --git a/admin/src/api/post/controllers/post.js b/admin/src/api/post/controllers/post.js index fb2c7ca8..965a9a14 100644 --- a/admin/src/api/post/controllers/post.js +++ b/admin/src/api/post/controllers/post.js @@ -83,4 +83,26 @@ module.exports = createCoreController("api::post.post", ({ strapi }) => ({ return this.transformResponse(posts); }, + + async autosave(ctx) { + const { postId, newData } = ctx.request.body; + let posts = await strapi.db.query("api::post.post").update({ + where: { id: postId }, + data: { + new_blog_content: newData, + }, + populate: { + author: { + populate: { + image: true, + }, + }, + tags: true, + category: true, + image: true, + }, + }); + + return this.transformResponse(posts); + }, })); diff --git a/admin/src/api/post/routes/custom.js b/admin/src/api/post/routes/custom.js index 2a0bd6f3..25fe0535 100644 --- a/admin/src/api/post/routes/custom.js +++ b/admin/src/api/post/routes/custom.js @@ -16,5 +16,13 @@ module.exports = { auth: false, }, }, + { + method: "PUT", + path: "/post/autosave", + handler: "post.autosave", + config: { + auth: false, + }, + }, ], }; diff --git a/admin/src/plugins/blog-editor/admin/src/components/editorjs/index.js b/admin/src/plugins/blog-editor/admin/src/components/editorjs/index.js index 7e1bf0c5..c3cc74be 100755 --- a/admin/src/plugins/blog-editor/admin/src/components/editorjs/index.js +++ b/admin/src/plugins/blog-editor/admin/src/components/editorjs/index.js @@ -43,6 +43,32 @@ const Editor = ({ onChange, name, value }) => { }, }; + async function autosave(newData) { + const urlPart = window.location.href.split("/"); + const postId = urlPart[urlPart.length - 1]; + + try { + await fetch("/v1/post/autosave", { + method: "PUT", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + postId, + newData, + }), + }) + .then((response) => { + // console.log(response); + }) + .catch((error) => { + console.log(error); + }); + } catch (error) { + console.error("Autosave error:", error); + } + } + return ( <>
{ onChange({ target: { name, value: newData } }); } else { onChange({ target: { name, value: JSON.stringify(newData) } }); + autosave(newData); } }} tools={{ ...requiredTools, ...customTools, ...customImageTool }}