2021-12-28 14:50:04 +05:00
|
|
|
import { parseHTML } from "./utils/html-parser";
|
2021-12-29 14:33:29 +05:00
|
|
|
import { decodeHTML5 } from "entities";
|
2021-12-28 14:50:04 +05:00
|
|
|
|
2020-12-05 15:26:54 +05:00
|
|
|
export const migrations = {
|
2021-10-23 10:59:03 +05:00
|
|
|
5.0: {},
|
2021-02-20 11:31:44 +05:00
|
|
|
5.1: {},
|
|
|
|
|
5.2: {
|
2021-12-22 10:24:01 +05:00
|
|
|
note: replaceDateEditedWithDateModified(),
|
|
|
|
|
notebook: replaceDateEditedWithDateModified(),
|
|
|
|
|
tag: replaceDateEditedWithDateModified(true),
|
|
|
|
|
attachment: replaceDateEditedWithDateModified(true),
|
|
|
|
|
trash: replaceDateEditedWithDateModified(),
|
2021-12-28 14:50:04 +05:00
|
|
|
tiny: (item) => {
|
|
|
|
|
item = replaceDateEditedWithDateModified()(item);
|
|
|
|
|
|
2022-07-07 13:17:55 +05:00
|
|
|
if (!item.data || item.data.iv) return migrations["5.3"].tiny(item);
|
2021-12-28 14:50:04 +05:00
|
|
|
|
|
|
|
|
item.data = removeToxClassFromChecklist(wrapTablesWithDiv(item.data));
|
2022-07-07 13:17:55 +05:00
|
|
|
return migrations["5.3"].tiny(item);
|
2021-12-28 14:50:04 +05:00
|
|
|
},
|
2021-12-22 10:24:01 +05:00
|
|
|
settings: replaceDateEditedWithDateModified(true),
|
|
|
|
|
},
|
|
|
|
|
5.3: {
|
2021-12-29 14:33:29 +05:00
|
|
|
tiny: (item) => {
|
2022-07-07 13:17:55 +05:00
|
|
|
if (!item.data || item.data.iv) return migrations["5.4"].tiny(item);
|
2021-12-29 14:33:29 +05:00
|
|
|
item.data = decodeWrappedTableHtml(item.data);
|
2022-07-07 13:17:55 +05:00
|
|
|
return migrations["5.4"].tiny(item);
|
2021-12-29 14:33:29 +05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
5.4: {
|
2022-07-07 13:17:55 +05:00
|
|
|
tiny: (item) => {
|
|
|
|
|
if (!item.data || item.data.iv) return item;
|
|
|
|
|
item.type = "tiptap";
|
|
|
|
|
item.data = tinyToTiptap(item.data);
|
|
|
|
|
return item;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
5.5: {
|
2020-12-06 10:52:00 +05:00
|
|
|
note: false,
|
2020-12-07 13:11:44 +05:00
|
|
|
notebook: false,
|
2020-12-06 10:52:00 +05:00
|
|
|
tag: false,
|
2021-10-28 13:37:55 +05:00
|
|
|
attachment: false,
|
2020-12-06 10:52:00 +05:00
|
|
|
trash: false,
|
2021-02-12 10:01:06 +05:00
|
|
|
tiny: false,
|
2022-07-07 13:17:55 +05:00
|
|
|
tiptap: false,
|
2020-12-06 10:52:00 +05:00
|
|
|
settings: false,
|
2020-12-05 15:26:54 +05:00
|
|
|
},
|
|
|
|
|
};
|
2021-12-22 10:24:01 +05:00
|
|
|
|
|
|
|
|
function replaceDateEditedWithDateModified(removeDateEditedProperty = false) {
|
|
|
|
|
return function (item) {
|
|
|
|
|
item.dateModified = item.dateEdited;
|
|
|
|
|
if (removeDateEditedProperty) delete item.dateEdited;
|
|
|
|
|
delete item.persistDateEdited;
|
|
|
|
|
return item;
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-12-28 14:50:04 +05:00
|
|
|
|
|
|
|
|
function wrapTablesWithDiv(html) {
|
|
|
|
|
const document = parseHTML(html);
|
|
|
|
|
const tables = document.getElementsByTagName("table");
|
|
|
|
|
for (let table of tables) {
|
|
|
|
|
table.setAttribute("contenteditable", "true");
|
2021-12-29 14:33:29 +05:00
|
|
|
const div = document.createElement("div");
|
|
|
|
|
div.setAttribute("contenteditable", "false");
|
|
|
|
|
div.innerHTML = table.outerHTML;
|
|
|
|
|
div.classList.add("table-container");
|
|
|
|
|
table.replaceWith(div);
|
2021-12-28 14:50:04 +05:00
|
|
|
}
|
|
|
|
|
return document.outerHTML || document.body.innerHTML;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function removeToxClassFromChecklist(html) {
|
|
|
|
|
const document = parseHTML(html);
|
|
|
|
|
const checklists = document.querySelectorAll(
|
|
|
|
|
".tox-checklist,.tox-checklist--checked"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for (let item of checklists) {
|
|
|
|
|
if (item.classList.contains("tox-checklist--checked"))
|
|
|
|
|
item.classList.replace("tox-checklist--checked", "checked");
|
|
|
|
|
else if (item.classList.contains("tox-checklist"))
|
|
|
|
|
item.classList.replace("tox-checklist", "checklist");
|
|
|
|
|
}
|
|
|
|
|
return document.outerHTML || document.body.innerHTML;
|
|
|
|
|
}
|
2021-12-29 14:33:29 +05:00
|
|
|
|
|
|
|
|
const regex = /<div class="table-container".*<\/table><\/div>/gm;
|
|
|
|
|
function decodeWrappedTableHtml(html) {
|
2021-12-31 09:18:06 +05:00
|
|
|
return html.replaceAll(regex, (match) => {
|
|
|
|
|
const html = decodeHTML5(match);
|
|
|
|
|
return html;
|
|
|
|
|
});
|
2021-12-29 14:33:29 +05:00
|
|
|
}
|
2022-07-07 13:17:55 +05:00
|
|
|
|
|
|
|
|
export function tinyToTiptap(html) {
|
2022-07-15 14:47:36 +05:00
|
|
|
if (typeof html !== "string") return html;
|
|
|
|
|
|
2022-07-07 13:17:55 +05:00
|
|
|
const document = parseHTML(html);
|
|
|
|
|
|
|
|
|
|
const tables = document.querySelectorAll("table");
|
|
|
|
|
for (const table of tables) {
|
|
|
|
|
table.removeAttribute("contenteditable");
|
2022-07-15 14:47:36 +05:00
|
|
|
if (
|
|
|
|
|
table.parentElement &&
|
|
|
|
|
table.parentElement.nodeName.toLowerCase() === "div"
|
|
|
|
|
) {
|
2022-07-07 13:17:55 +05:00
|
|
|
table.parentElement.replaceWith(table);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const images = document.querySelectorAll("p > img");
|
|
|
|
|
for (const image of images) {
|
|
|
|
|
image.parentElement.replaceWith(image.cloneNode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const breaks = document.querySelectorAll("br");
|
|
|
|
|
for (const br of breaks) {
|
|
|
|
|
br.remove();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const paragraphs = document.querySelectorAll("p");
|
|
|
|
|
for (const p of paragraphs) {
|
|
|
|
|
if (!p.childNodes.length) p.remove();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bogus = document.querySelectorAll("[data-mce-bogus]");
|
|
|
|
|
for (const element of bogus) {
|
|
|
|
|
element.remove();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const attributes = document.querySelectorAll(
|
|
|
|
|
"[data-mce-href], [data-mce-flag]"
|
|
|
|
|
);
|
|
|
|
|
for (const element of attributes) {
|
|
|
|
|
element.removeAttribute("data-mce-href");
|
|
|
|
|
element.removeAttribute("data-mce-flag");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return document.body.innerHTML;
|
|
|
|
|
}
|