Skip to content

Build your personal knowledge base with Trilium Notes

client/note type conversion: fix some issues

Adorian Doran1989b29fa020
diff --git a/apps/client/src/widgets/bulk_actions/note/convert_note.tsx b/apps/client/src/widgets/bulk_actions/note/convert_note.tsx
index 69ff2a31fd..42c410a4b6 100644
--- a/apps/client/src/widgets/bulk_actions/note/convert_note.tsx
+++ b/apps/client/src/widgets/bulk_actions/note/convert_note.tsx
@@ -1,22 +1,26 @@
import { NOTE_CONVERSION_IDS, NoteConversionId, RISKY_NOTE_CONVERSION_IDS } from "@triliumnext/commons";
-import { useEffect, useState } from "preact/hooks";
+import { useState } from "preact/hooks";
import { t } from "../../../services/i18n";
import FormSelect from "../../react/FormSelect";
-import { useSpacedUpdate } from "../../react/hooks";
import AbstractBulkAction, { ActionDefinition } from "../abstract_bulk_action";
import BulkAction from "../BulkAction";
-/** Human-readable label for each conversion id, in the order they appear in the combo box. */
-const CONVERSION_OPTIONS: { value: NoteConversionId | ""; title: string }[] = [
- { value: "", title: t("convert_note.choose_conversion") },
- ...NOTE_CONVERSION_IDS.map((value) => ({ value, title: t(`convert_note.conversion_${value}`) }))
-];
-
function ConvertNoteBulkActionComponent({ bulkAction, actionDef }: { bulkAction: AbstractBulkAction, actionDef: ActionDefinition }) {
const [ conversion, setConversion ] = useState<string>(actionDef.conversion ?? "");
- const spacedUpdate = useSpacedUpdate(() => bulkAction.saveAction({ conversion }));
- useEffect(() => spacedUpdate.scheduleUpdate(), [ conversion ]);
+
+ // Evaluate labels on render so they follow runtime language changes.
+ const conversionOptions: { value: NoteConversionId | ""; title: string }[] = [
+ { value: "", title: t("convert_note.choose_conversion") },
+ ...NOTE_CONVERSION_IDS.map((value) => ({ value, title: t(`convert_note.conversion_${value}`) }))
+ ];
+
+ // Persist the choice immediately (not debounced) so executing right after selecting doesn't
+ // run against a stale/empty conversion and silently skip every note.
+ function onChange(value: string) {
+ setConversion(value);
+ void bulkAction.saveAction({ conversion: value });
+ }
return (
<BulkAction
@@ -25,11 +29,11 @@ function ConvertNoteBulkActionComponent({ bulkAction, actionDef }: { bulkAction:
helpText={<p>{t("convert_note.help_text")}</p>}
>
<FormSelect
- values={CONVERSION_OPTIONS}
+ values={conversionOptions}
keyProperty="value"
titleProperty="title"
currentValue={conversion}
- onChange={setConversion}
+ onChange={onChange}
/>
</BulkAction>
);
@@ -47,7 +51,7 @@ export default class ConvertNoteBulkAction extends AbstractBulkAction {
if (!conversion) {
return null;
}
- return RISKY_NOTE_CONVERSION_IDS.includes(conversion)
+ return RISKY_NOTE_CONVERSION_IDS.includes(conversion as NoteConversionId)
? t("convert_note.warning_risky")
: t("convert_note.warning");
}
diff --git a/apps/client/src/widgets/dialogs/bulk_actions.tsx b/apps/client/src/widgets/dialogs/bulk_actions.tsx
index f32227c0fb..785619b09e 100644
--- a/apps/client/src/widgets/dialogs/bulk_actions.tsx
+++ b/apps/client/src/widgets/dialogs/bulk_actions.tsx
@@ -9,7 +9,7 @@ import Button from "../react/Button";
import bulk_action from "../../services/bulk_action";
import dialog from "../../services/dialog";
import toast from "../../services/toast";
-import RenameNoteBulkAction from "../bulk_actions/note/rename_note";
+import AbstractBulkAction from "../bulk_actions/abstract_bulk_action";
import FNote from "../../entities/fnote";
import froca from "../../services/froca";
import { useTriliumEvent } from "../react/hooks";
@@ -19,7 +19,7 @@ export default function BulkActionsDialog() {
const [ bulkActionNote, setBulkActionNote ] = useState<FNote | null>();
const [ includeDescendants, setIncludeDescendants ] = useState(false);
const [ affectedNoteCount, setAffectedNoteCount ] = useState(0);
- const [ existingActions, setExistingActions ] = useState<RenameNoteBulkAction[]>([]);
+ const [ existingActions, setExistingActions ] = useState<AbstractBulkAction[]>([]);
const [ shown, setShown ] = useState(false);
useTriliumEvent("openBulkActionsDialog", async ({ selectedOrActiveNoteIds }) => {
@@ -113,7 +113,7 @@ function AvailableActionsList() {
</table>;
}
-function ExistingActionsList({ existingActions }: { existingActions?: RenameNoteBulkAction[] }) {
+function ExistingActionsList({ existingActions }: { existingActions?: AbstractBulkAction[] }) {
return (
<table class="bulk-existing-action-list">
{ existingActions
diff --git a/packages/trilium-core/src/services/note_format_conversion.ts b/packages/trilium-core/src/services/note_format_conversion.ts
index 022c49b521..11a21e8963 100644
--- a/packages/trilium-core/src/services/note_format_conversion.ts
+++ b/packages/trilium-core/src/services/note_format_conversion.ts
@@ -5,6 +5,7 @@ import { ValidationError } from "../errors.js";
import mdExportService from "./export/markdown.js";
import markdownImportService from "./import/markdown.js";
import noteService from "./notes.js";
+import { getSql } from "./sql/index.js";
import type BNote from "../becca/entities/bnote.js";
/** The MIME used for the Markdown note type, matching the note-type registry (`NOTE_TYPES`). */
@@ -106,26 +107,33 @@ export function convertNoteByConversionId(note: BNote, conversionId: string): bo
/** Saves the pre-conversion revision, transforms the content, and switches the note's type/mime. */
function performConversion(note: BNote, sourceFormat: SourceFormat) {
- const content = note.getContent();
- if (typeof content !== "string") {
+ const rawContent = note.getContent();
+ if (rawContent != null && typeof rawContent !== "string") {
throw new ValidationError(`Note '${note.noteId}' does not have textual content.`);
}
+ // A brand-new or empty note may have no content; treat it as an empty document rather than failing.
+ const content = rawContent ?? "";
// Snapshot the current (pre-conversion) state under a descriptive name.
const description = sourceFormat === "markdown"
? t("note_conversion.revision_before_text")
: t("note_conversion.revision_before_markdown");
- note.saveRevision({ description, source: "manual" });
const { content: newContent, type, mime } = convertNoteContent(sourceFormat, content, note.title);
- // Change the type/mime before writing content so type-specific processing (link extraction
- // in `updateNoteData`) runs against the new format.
- note.type = type;
- note.mime = mime;
- note.save();
+ // Persist the revision, the type/mime switch and the converted content in a single transaction,
+ // so a sync peer never observes the note with its new type but old (unconverted) content.
+ getSql().transactional(() => {
+ note.saveRevision({ description, source: "manual" });
- noteService.updateNoteData(note.noteId, newContent);
+ // Change the type/mime before writing content so type-specific processing (link extraction
+ // in `updateNoteData`) runs against the new format.
+ note.type = type;
+ note.mime = mime;
+ note.save();
+
+ noteService.updateNoteData(note.noteId, newContent);
+ });
}
export default {