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); 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(); if (!password) password = await this.getVaultPassword();
await this.encryptContent( await this.encryptContent(
decryptedContent, decryptedContent,

View File

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

View File

@@ -88,7 +88,7 @@ export class Notes implements ICollection {
logger.debug("saving content", { id }); logger.debug("saving content", { id });
const { type, data } = item.content; const { type, data } = item.content;
const content = getContentFromData(type, data); const content = await getContentFromData(type, data);
if (!content) throw new Error("Invalid content type."); if (!content) throw new Error("Invalid content type.");
headline = getNoteHeadline(content); headline = getNoteHeadline(content);
@@ -293,7 +293,7 @@ export class Notes implements ICollection {
false false
) )
: contentItem; : contentItem;
const content = getContentFromData(type, data); const content = await getContentFromData(type, data);
return format === "html" return format === "html"
? content.toHTML() ? content.toHTML()
: format === "md" : format === "md"
@@ -420,15 +420,16 @@ export class Notes implements ICollection {
const content = await this.db.content.findByNoteId(id); const content = await this.db.content.findByNoteId(id);
if (!content || content.locked) return []; if (!content || content.locked) return [];
return getContentFromData(content.type, content.data).extract("blocks") return (await getContentFromData(content.type, content.data)).extract(
.blocks; "blocks"
).blocks;
} }
async contentBlocksWithLinks(id: string) { async contentBlocksWithLinks(id: string) {
const content = await this.db.content.findByNoteId(id); const content = await this.db.content.findByNoteId(id);
if (!content || content.locked) return []; if (!content || content.locked) return [];
return getContentFromData(content.type, content.data).extract( return (await getContentFromData(content.type, content.data)).extract(
"blocksWithLink" "blocksWithLink"
).blocks; ).blocks;
} }
@@ -437,7 +438,7 @@ export class Notes implements ICollection {
const content = await this.db.content.findByNoteId(id); const content = await this.db.content.findByNoteId(id);
if (!content || content.locked) return []; if (!content || content.locked) return [];
return getContentFromData(content.type, content.data).extract( return (await getContentFromData(content.type, content.data)).extract(
"internalLinks" "internalLinks"
).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 { 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) { switch (type) {
case "tiptap": case "tiptap": {
const { Tiptap } = await import("./tiptap.js");
return new Tiptap(data); return new Tiptap(data);
}
default: default:
throw new Error( throw new Error(
`Unknown content type: "${type}". Please report this error at support@streetwriters.co.` `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 type { SyncOptions } from "./api/sync/index.js";
export { sanitizeTag } from "./collections/tags.js"; export { sanitizeTag } from "./collections/tags.js";
export { default as DataURL } from "./utils/dataurl.js"; export { default as DataURL } from "./utils/dataurl.js";
export { type ResolveInternalLink } from "./content-types/tiptap.js";