Skip to content

Build your personal knowledge base with Trilium Notes

feat(react/bulk_actions): port update relation target

Elian Doran18c6fe7ebdb2
diff --git a/apps/client/src/widgets/bulk_actions/relation/update_relation_target.ts b/apps/client/src/widgets/bulk_actions/relation/update_relation_target.ts
deleted file mode 100644
index b75dd40fa5..0000000000
--- a/apps/client/src/widgets/bulk_actions/relation/update_relation_target.ts
+++ /dev/null
@@ -1,74 +0,0 @@
-import SpacedUpdate from "../../../services/spaced_update.js";
-import AbstractBulkAction from "../abstract_bulk_action.js";
-import noteAutocompleteService from "../../../services/note_autocomplete.js";
-import { t } from "../../../services/i18n.js";
-
-const TPL = /*html*/`
-<tr>
- <td colspan="2">
- <div style="display: flex; align-items: center">
- <div style="margin-right: 10px;" class="text-nowrap">${t("update_relation_target.update_relation")}</div>
-
- <input type="text"
- class="form-control relation-name"
- placeholder="${t("update_relation_target.relation_name")}"
- pattern="[\\p{L}\\p{N}_:]+"
- style="flex-shrink: 3"
- title="${t("update_relation_target.allowed_characters")}"/>
-
- <div style="margin-right: 10px; margin-left: 10px;" class="text-nowrap">${t("update_relation_target.to")}</div>
-
- <div class="input-group" style="flex-shrink: 2">
- <input type="text" class="form-control target-note" placeholder="${t("update_relation_target.target_note")}"/>
- </div>
- </div>
- </td>
- <td class="button-column">
- <div class="dropdown help-dropdown">
- <span class="bx bx-help-circle icon-action" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
- <div class="dropdown-menu dropdown-menu-right p-4">
- <p>${t("update_relation_target.on_all_matched_notes")}:</p>
-
- <ul style="margin-bottom: 0;">
- <li>${t("update_relation_target.change_target_note")}</li>
- </ul>
- </div>
- </div>
-
- <span class="bx bx-x icon-action action-conf-del"></span>
- </td>
-</tr>`;
-
-export default class UpdateRelationTargetBulkAction extends AbstractBulkAction {
- static get actionName() {
- return "updateRelationTarget";
- }
- static get actionTitle() {
- return t("update_relation_target.update_relation_target");
- }
-
- doRender() {
- const $action = $(TPL);
-
- const $relationName = $action.find(".relation-name");
- $relationName.val(this.actionDef.relationName || "");
-
- const $targetNote = $action.find(".target-note");
- noteAutocompleteService.initNoteAutocomplete($targetNote);
- $targetNote.setNote(this.actionDef.targetNoteId);
-
- $targetNote.on("autocomplete:closed", () => spacedUpdate.scheduleUpdate());
-
- const spacedUpdate = new SpacedUpdate(async () => {
- await this.saveAction({
- relationName: $relationName.val(),
- targetNoteId: $targetNote.getSelectedNoteId()
- });
- }, 1000);
-
- $relationName.on("input", () => spacedUpdate.scheduleUpdate());
- $targetNote.on("input", () => spacedUpdate.scheduleUpdate());
-
- return $action;
- }
-}
diff --git a/apps/client/src/widgets/bulk_actions/relation/update_relation_target.tsx b/apps/client/src/widgets/bulk_actions/relation/update_relation_target.tsx
new file mode 100644
index 0000000000..b2d5935442
--- /dev/null
+++ b/apps/client/src/widgets/bulk_actions/relation/update_relation_target.tsx
@@ -0,0 +1,60 @@
+import AbstractBulkAction, { ActionDefinition } from "../abstract_bulk_action.js";
+import { t } from "../../../services/i18n.js";
+import BulkAction, { BulkActionText } from "../BulkAction.jsx";
+import FormTextBox from "../../react/FormTextBox.jsx";
+import NoteAutocomplete from "../../react/NoteAutocomplete.jsx";
+import { useEffect, useState } from "preact/hooks";
+import { useSpacedUpdate } from "../../react/hooks.jsx";
+
+function UpdateRelationTargetComponent({ bulkAction, actionDef }: { bulkAction: AbstractBulkAction, actionDef: ActionDefinition }) {
+ const [ relationName, setRelationName ] = useState(actionDef.relationName);
+ const [ targetNoteId, setTargetNoteId ] = useState(actionDef.targetNoteId);
+ const spacedUpdate = useSpacedUpdate(() => bulkAction.saveAction({ relationName, targetNoteId }));
+ useEffect(() => spacedUpdate.scheduleUpdate(), [ relationName, targetNoteId ]);
+
+ return (
+ <BulkAction
+ bulkAction={bulkAction}
+ label={t("update_relation_target.update_relation")}
+ helpText={<>
+ <p>{t("update_relation_target.on_all_matched_notes")}:</p>
+
+ <ul style="margin-bottom: 0;">
+ <li>{t("update_relation_target.change_target_note")}</li>
+ </ul>
+ </>}
+ >
+ <FormTextBox
+ placeholder={t("update_relation_target.relation_name")}
+ pattern="[\\p{L}\\p{N}_:]+"
+ style={{ flexShrink: 3 }}
+ title={t("update_relation_target.allowed_characters")}
+ currentValue={relationName} onChange={setRelationName}
+ />
+
+ <BulkActionText text={t("update_relation_target.to")} />
+
+ <NoteAutocomplete
+ placeholder={t("update_relation_target.target_note")}
+ containerStyle={{ flexShrink: 2 }}
+ noteId={targetNoteId} noteIdChanged={setTargetNoteId}
+ />
+ </BulkAction>
+ )
+}
+
+export default class UpdateRelationTargetBulkAction extends AbstractBulkAction {
+
+ static get actionName() {
+ return "updateRelationTarget";
+ }
+
+ static get actionTitle() {
+ return t("update_relation_target.update_relation_target");
+ }
+
+ doRender() {
+ return <UpdateRelationTargetComponent bulkAction={this} actionDef={this.actionDef} />
+ }
+
+}
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsx
index 926a9e3282..580c000025 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx
@@ -3,12 +3,14 @@ import { t } from "../../services/i18n";
import { useEffect } from "react";
import note_autocomplete, { Options, type Suggestion } from "../../services/note_autocomplete";
import type { RefObject } from "preact";
+import { CSSProperties } from "preact/compat";
interface NoteAutocompleteProps {
inputRef?: RefObject<HTMLInputElement>;
text?: string;
placeholder?: string;
container?: RefObject<HTMLDivElement>;
+ containerStyle?: CSSProperties;
opts?: Omit<Options, "container">;
onChange?: (suggestion: Suggestion | null) => void;
onTextChange?: (text: string) => void;
@@ -16,7 +18,7 @@ interface NoteAutocompleteProps {
noteId?: string;
}
-export default function NoteAutocomplete({ inputRef: _ref, text, placeholder, onChange, onTextChange, container, opts, noteId, noteIdChanged }: NoteAutocompleteProps) {
+export default function NoteAutocomplete({ inputRef: _ref, text, placeholder, onChange, onTextChange, container, containerStyle, opts, noteId, noteIdChanged }: NoteAutocompleteProps) {
const ref = _ref ?? useRef<HTMLInputElement>(null);
useEffect(() => {
@@ -67,7 +69,7 @@ export default function NoteAutocomplete({ inputRef: _ref, text, placeholder, on
}, [text]);
return (
- <div className="input-group">
+ <div className="input-group" style={containerStyle}>
<input
ref={ref}
className="note-autocomplete form-control"