core: lazy load content-type

This commit is contained in:
Abdullah Atta
2024-11-12 13:12:04 +05:00
committed by Abdullah Atta
parent 5f1f308540
commit ece142e8d8
5 changed files with 30 additions and 21 deletions

View File

@@ -352,7 +352,7 @@ export default class Vault {
}
const decryptedContent = await this.decryptContent(content, password);
if (this.db.content.preProcess(decryptedContent)) {
if (await this.db.content.preProcess(decryptedContent)) {
if (!password) password = await this.getVaultPassword();
await this.encryptContent(
decryptedContent,

View File

@@ -174,7 +174,7 @@ export class Content implements ICollection {
async get(id: string) {
const content = await this.collection.get(id);
if (!content || isDeleted(content)) return;
if (!content.locked && this.preProcess(content)) {
if (!content.locked && (await this.preProcess(content))) {
await this.collection.update([content.id], content, { modify: false });
}
return content;
@@ -242,7 +242,7 @@ export class Content implements ICollection {
.selectAll()
.executeTakeFirst()) as ContentItem;
if (!content || isDeleted(content)) return;
if (!content.locked && this.preProcess(content)) {
if (!content.locked && (await this.preProcess(content))) {
await this.collection.update([content.id], content, { modify: false });
}
return content;
@@ -267,7 +267,10 @@ export class Content implements ICollection {
contentItem: { type: ContentType; data: string },
notify = true
) {
const content = getContentFromData(contentItem.type, contentItem.data);
const content = await getContentFromData(
contentItem.type,
contentItem.data
);
if (!content) return contentItem;
contentItem.data = await content.insertMedia(async (hashes) => {
const attachments = await this.db.attachments.all
@@ -300,13 +303,16 @@ export class Content implements ICollection {
async removeAttachments(id: string, hashes: string[]) {
const contentItem = await this.get(id);
if (!contentItem || isCipher(contentItem.data)) return;
const content = getContentFromData(contentItem.type, contentItem.data);
const content = await getContentFromData(
contentItem.type,
contentItem.data
);
if (!content) return;
contentItem.data = content.removeAttachments(hashes);
await this.add(contentItem);
}
preProcess(content: NoteContent<false>) {
async preProcess(content: NoteContent<false>) {
let changed = false;
// #MIGRATION: convert tiny to tiptap
@@ -318,9 +324,8 @@ export class Content implements ICollection {
// add block id on all appropriate nodes
if (!content.data.includes("data-block-id")) {
content.data = getContentFromData(
content.type,
content.data
content.data = (
await getContentFromData(content.type, content.data)
).insertBlockIds();
changed = true;
}
@@ -329,7 +334,10 @@ export class Content implements ICollection {
}
async postProcess(contentItem: NoteContent<false> & { noteId: string }) {
const content = getContentFromData(contentItem.type, contentItem.data);
const content = await getContentFromData(
contentItem.type,
contentItem.data
);
if (!content) return contentItem.data;
const { data, hashes, internalLinks } = await content.postProcess(
this.db.attachments.save.bind(this.db.attachments)

View File

@@ -88,7 +88,7 @@ export class Notes implements ICollection {
logger.debug("saving content", { id });
const { type, data } = item.content;
const content = getContentFromData(type, data);
const content = await getContentFromData(type, data);
if (!content) throw new Error("Invalid content type.");
headline = getNoteHeadline(content);
@@ -293,7 +293,7 @@ export class Notes implements ICollection {
false
)
: contentItem;
const content = getContentFromData(type, data);
const content = await getContentFromData(type, data);
return format === "html"
? content.toHTML()
: format === "md"
@@ -420,15 +420,16 @@ export class Notes implements ICollection {
const content = await this.db.content.findByNoteId(id);
if (!content || content.locked) return [];
return getContentFromData(content.type, content.data).extract("blocks")
.blocks;
return (await getContentFromData(content.type, content.data)).extract(
"blocks"
).blocks;
}
async contentBlocksWithLinks(id: string) {
const content = await this.db.content.findByNoteId(id);
if (!content || content.locked) return [];
return getContentFromData(content.type, content.data).extract(
return (await getContentFromData(content.type, content.data)).extract(
"blocksWithLink"
).blocks;
}
@@ -437,7 +438,7 @@ export class Notes implements ICollection {
const content = await this.db.content.findByNoteId(id);
if (!content || content.locked) return [];
return getContentFromData(content.type, content.data).extract(
return (await getContentFromData(content.type, content.data)).extract(
"internalLinks"
).internalLinks;
}

View File

@@ -18,17 +18,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ContentType } from "../types.js";
import { Tiptap } from "./tiptap.js";
export function getContentFromData(type: ContentType, data: string) {
export async function getContentFromData(type: ContentType, data: string) {
switch (type) {
case "tiptap":
case "tiptap": {
const { Tiptap } = await import("./tiptap.js");
return new Tiptap(data);
}
default:
throw new Error(
`Unknown content type: "${type}". Please report this error at support@streetwriters.co.`
);
}
}
export * from "./tiptap.js";

View File

@@ -43,3 +43,4 @@ export { VAULT_ERRORS } from "./api/vault.js";
export type { SyncOptions } from "./api/sync/index.js";
export { sanitizeTag } from "./collections/tags.js";
export { default as DataURL } from "./utils/dataurl.js";
export { type ResolveInternalLink } from "./content-types/tiptap.js";