Skip to content

Build your personal knowledge base with Trilium Notes

refactor: ensure only the changed lines are shown

Zaki Ur Rehmanf64b0290092e
diff --git a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx
index dd5eb8895b..1e5bb77500 100644
--- a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx
+++ b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx
@@ -322,81 +322,99 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
setNeedsSaving(oldValue !== newValue);
setError(undefined);
}}
- // onClick={(e, pos) => {
- // if (pos && pos.textNode && pos.textNode.data) {
- // const clickIndex = getClickIndex(pos);
-
- // let parsedAttrs: Attribute[];
-
- // try {
- // parsedAttrs = attribute_parser.lexAndParse(getPreprocessedData(currentValueRef.current), true);
- // } catch (e: unknown) {
- // // the input is incorrect because the user messed up with it and now needs to fix it manually
- // console.log(e);
- // return null;
- // }
- onClick={(e) => {
- // Prevent automatic hiding of the context menu due to the button being clicked.
- e.stopPropagation();
- if (isMenuOpen) {
- // If we re-show the menu, ContextMenu.show() will call hide()
- // and immediately trigger onHide. Suppress that transient hide.
- suppressNextOnHide.current = true;
- }
- setIsMenuOpen(true);
-
- contextMenu.show<AttributeCommandNames>({
- x: e.pageX,
- y: e.pageY,
- orientation: "left",
-
- items: [
- {
- title: t(
- "attribute_editor.add_new_label",
- ),
- command: "addNewLabel",
- uiIcon: "bx bx-hash",
- },
- {
- title: t(
- "attribute_editor.add_new_relation",
- ),
- command: "addNewRelation",
- uiIcon: "bx bx-transfer",
- },
- { kind: "separator" },
- {
- title: t(
- "attribute_editor.add_new_label_definition",
- ),
- command:
- "addNewLabelDefinition",
- uiIcon: "bx bx-empty",
- },
- {
- title: t(
- "attribute_editor.add_new_relation_definition",
- ),
- command:
- "addNewRelationDefinition",
- uiIcon: "bx bx-empty",
- },
- ],
- selectMenuItemHandler: (item) =>
- handleAddNewAttributeCommand(
- item.command,
- ),
- onHide: () => {
- if (suppressNextOnHide.current) {
- suppressNextOnHide.current = false;
- return;
+ onClick={(e, pos) => {
+ if (pos && pos.textNode && pos.textNode.data) {
+ const clickIndex = getClickIndex(pos);
+
+ let parsedAttrs: Attribute[];
+
+ try {
+ parsedAttrs = attribute_parser.lexAndParse(getPreprocessedData(currentValueRef.current), true);
+ } catch (e: unknown) {
+ // the input is incorrect because the user messed up with it and now needs to fix it manually
+ console.log(e);
+ return null;
+ }
+
+ let matchedAttr: Attribute | null = null;
+
+ for (const attr of parsedAttrs) {
+ if (attr.startIndex !== undefined && clickIndex > attr.startIndex &&
+ attr.endIndex !== undefined && clickIndex <= attr.endIndex) {
+ matchedAttr = attr;
+ break;
}
- setIsMenuOpen(false);
- },
- });
+ }
+
+ setTimeout(() => {
+ if (matchedAttr) {
+ attributeDetailWidget.showAttributeDetail({
+ allAttributes: parsedAttrs,
+ attribute: matchedAttr,
+ isOwned: true,
+ x: e.pageX,
+ y: e.pageY
+ });
+ setState("showAttributeDetail");
+ } else {
+ setState("showHelpTooltip");
+ }
+ }, 100);
+ } else {
+ setState("showHelpTooltip");
+ }
}}
+ onKeyDown={() => attributeDetailWidget.hide()}
+ onBlur={() => save()}
+ onInitialized={() => editorRef.current?.focus()}
+ disableNewlines disableSpellcheck
/>
+
+ <div className="attribute-editor-buttons">
+ { needsSaving && <ActionButton
+ icon="bx bx-save"
+ className="save-attributes-button tn-tool-button"
+ text={escapeQuotes(t("attribute_editor.save_attributes"))}
+ onClick={save}
+ /> }
+
+ <ActionButton
+ icon="bx bx-plus"
+ className="add-new-attribute-button tn-tool-button"
+ text={escapeQuotes(t("attribute_editor.add_a_new_attribute"))}
+ onClick={(e) => {
+ // Prevent automatic hiding of the context menu due to the button being clicked.
+ e.stopPropagation();
+ if (isMenuOpen) {
+ // If we re-show the menu, ContextMenu.show() will call hide()
+ // and immediately trigger onHide. Suppress that transient hide.
+ suppressNextOnHide.current = true;
+ }
+ setIsMenuOpen(true);
+
+ contextMenu.show<AttributeCommandNames>({
+ x: e.pageX,
+ y: e.pageY,
+ orientation: "left",
+ items: [
+ { title: t("attribute_editor.add_new_label"), command: "addNewLabel", uiIcon: "bx bx-hash" },
+ { title: t("attribute_editor.add_new_relation"), command: "addNewRelation", uiIcon: "bx bx-transfer" },
+ { kind: "separator" },
+ { title: t("attribute_editor.add_new_label_definition"), command: "addNewLabelDefinition", uiIcon: "bx bx-empty" },
+ { title: t("attribute_editor.add_new_relation_definition"), command: "addNewRelationDefinition", uiIcon: "bx bx-empty" }
+ ],
+ selectMenuItemHandler: (item) => handleAddNewAttributeCommand(item.command),
+ onHide: () => {
+ if (suppressNextOnHide.current) {
+ suppressNextOnHide.current = false;
+ return;
+ }
+ setIsMenuOpen(false);
+ },
+ });
+ }}
+ />
+ </div>
</div>
{ error && (