mirror of
https://github.com/zadam/trilium.git
synced 2025-11-12 00:05:50 +01:00
chore(react/ribbon): load current attributes in editor
This commit is contained in:
@@ -289,22 +289,7 @@ export default class AttributeEditorWidget extends NoteContextAwareWidget implem
|
|||||||
$el.text(title);
|
$el.text(title);
|
||||||
}
|
}
|
||||||
|
|
||||||
async refreshWithNote(note: FNote) {
|
|
||||||
await this.renderOwnedAttributes(note.getOwnedAttributes(), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
async renderOwnedAttributes(ownedAttributes: FAttribute[], saved: boolean) {
|
async renderOwnedAttributes(ownedAttributes: FAttribute[], saved: boolean) {
|
||||||
// attrs are not resorted if position changes after the initial load
|
|
||||||
ownedAttributes.sort((a, b) => a.position - b.position);
|
|
||||||
|
|
||||||
let htmlAttrs = (await attributeRenderer.renderAttributes(ownedAttributes, true)).html();
|
|
||||||
|
|
||||||
if (htmlAttrs.length > 0) {
|
|
||||||
htmlAttrs += " ";
|
|
||||||
}
|
|
||||||
|
|
||||||
this.textEditor.setData(htmlAttrs);
|
|
||||||
|
|
||||||
if (saved) {
|
if (saved) {
|
||||||
this.lastSavedContent = this.textEditor.getData();
|
this.lastSavedContent = this.textEditor.getData();
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { CKTextEditor, type AttributeEditor, type EditorConfig, type ModelPositi
|
|||||||
import { useEffect, useRef } from "preact/compat";
|
import { useEffect, useRef } from "preact/compat";
|
||||||
|
|
||||||
interface CKEditorOpts {
|
interface CKEditorOpts {
|
||||||
|
currentValue?: string;
|
||||||
className: string;
|
className: string;
|
||||||
tabIndex?: number;
|
tabIndex?: number;
|
||||||
config: EditorConfig;
|
config: EditorConfig;
|
||||||
@@ -12,7 +13,7 @@ interface CKEditorOpts {
|
|||||||
onClick?: (pos?: ModelPosition | null) => void;
|
onClick?: (pos?: ModelPosition | null) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function CKEditor({ className, tabIndex, editor, config, disableNewlines, disableSpellcheck, onChange, onClick }: CKEditorOpts) {
|
export default function CKEditor({ currentValue, className, tabIndex, editor, config, disableNewlines, disableSpellcheck, onChange, onClick }: CKEditorOpts) {
|
||||||
const editorContainerRef = useRef<HTMLDivElement>(null);
|
const editorContainerRef = useRef<HTMLDivElement>(null);
|
||||||
const textEditorRef = useRef<CKTextEditor>(null);
|
const textEditorRef = useRef<CKTextEditor>(null);
|
||||||
|
|
||||||
@@ -44,9 +45,18 @@ export default function CKEditor({ className, tabIndex, editor, config, disableN
|
|||||||
if (onChange) {
|
if (onChange) {
|
||||||
textEditor.model.document.on("change:data", onChange);
|
textEditor.model.document.on("change:data", onChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (currentValue) {
|
||||||
|
textEditor.setData(currentValue);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!textEditorRef.current) return;
|
||||||
|
textEditorRef.current.setData(currentValue ?? "");
|
||||||
|
}, [ currentValue ]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
ref={editorContainerRef}
|
ref={editorContainerRef}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { TabContext } from "./ribbon-interface";
|
|||||||
export default function OwnedAttributesTab({ note }: TabContext) {
|
export default function OwnedAttributesTab({ note }: TabContext) {
|
||||||
return (
|
return (
|
||||||
<div className="attribute-list">
|
<div className="attribute-list">
|
||||||
<AttributeEditor />
|
{ note && <AttributeEditor note={note} /> }
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,9 @@ import server from "../../../services/server";
|
|||||||
import note_autocomplete, { Suggestion } from "../../../services/note_autocomplete";
|
import note_autocomplete, { Suggestion } from "../../../services/note_autocomplete";
|
||||||
import CKEditor from "../../react/CKEditor";
|
import CKEditor from "../../react/CKEditor";
|
||||||
import { useTooltip } from "../../react/hooks";
|
import { useTooltip } from "../../react/hooks";
|
||||||
|
import FAttribute from "../../../entities/fattribute";
|
||||||
|
import attribute_renderer from "../../../services/attribute_renderer";
|
||||||
|
import FNote from "../../../entities/fnote";
|
||||||
|
|
||||||
const HELP_TEXT = `
|
const HELP_TEXT = `
|
||||||
<p>${t("attribute_editor.help_text_body1")}</p>
|
<p>${t("attribute_editor.help_text_body1")}</p>
|
||||||
@@ -58,9 +61,10 @@ const mentionSetup: MentionFeed[] = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
export default function AttributeEditor() {
|
export default function AttributeEditor({ note }: { note: FNote }) {
|
||||||
|
|
||||||
const [ state, setState ] = useState<"normal" | "showHelpTooltip" | "showAttributeDetail">();
|
const [ state, setState ] = useState<"normal" | "showHelpTooltip" | "showAttributeDetail">();
|
||||||
|
const [ currentValue, setCurrentValue ] = useState<string>("");
|
||||||
const wrapperRef = useRef<HTMLDivElement>(null);
|
const wrapperRef = useRef<HTMLDivElement>(null);
|
||||||
const { showTooltip, hideTooltip } = useTooltip(wrapperRef, {
|
const { showTooltip, hideTooltip } = useTooltip(wrapperRef, {
|
||||||
trigger: "focus",
|
trigger: "focus",
|
||||||
@@ -77,6 +81,23 @@ export default function AttributeEditor() {
|
|||||||
hideTooltip();
|
hideTooltip();
|
||||||
}
|
}
|
||||||
}, [ state ]);
|
}, [ state ]);
|
||||||
|
|
||||||
|
async function renderOwnedAttributes(ownedAttributes: FAttribute[], saved: boolean) {
|
||||||
|
// attrs are not resorted if position changes after the initial load
|
||||||
|
ownedAttributes.sort((a, b) => a.position - b.position);
|
||||||
|
|
||||||
|
let htmlAttrs = (await attribute_renderer.renderAttributes(ownedAttributes, true)).html();
|
||||||
|
|
||||||
|
if (htmlAttrs.length > 0) {
|
||||||
|
htmlAttrs += " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
setCurrentValue(htmlAttrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
renderOwnedAttributes(note.getOwnedAttributes(), true);
|
||||||
|
}, [ note ]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={wrapperRef} style="position: relative; padding-top: 10px; padding-bottom: 10px">
|
<div ref={wrapperRef} style="position: relative; padding-top: 10px; padding-bottom: 10px">
|
||||||
@@ -84,6 +105,7 @@ export default function AttributeEditor() {
|
|||||||
className="attribute-list-editor"
|
className="attribute-list-editor"
|
||||||
tabIndex={200}
|
tabIndex={200}
|
||||||
editor={CKEditorAttributeEditor}
|
editor={CKEditorAttributeEditor}
|
||||||
|
currentValue={currentValue}
|
||||||
config={{
|
config={{
|
||||||
toolbar: { items: [] },
|
toolbar: { items: [] },
|
||||||
placeholder: t("attribute_editor.placeholder"),
|
placeholder: t("attribute_editor.placeholder"),
|
||||||
|
|||||||
Reference in New Issue
Block a user