2025-11-23 10:46:56 +02:00
|
|
|
import { MutableRef, useEffect, useRef, useState } from "preact/hooks";
|
2025-11-22 22:10:51 +02:00
|
|
|
import "./PromotedAttributes.css";
|
2025-11-23 10:46:56 +02:00
|
|
|
import { useNoteContext, useNoteLabel, useUniqueName } from "./react/hooks";
|
2025-11-22 22:10:51 +02:00
|
|
|
import { Attribute } from "../services/attribute_parser";
|
2025-11-22 22:25:32 +02:00
|
|
|
import FAttribute from "../entities/fattribute";
|
2025-11-23 10:22:58 +02:00
|
|
|
import clsx from "clsx";
|
|
|
|
|
import { t } from "../services/i18n";
|
|
|
|
|
import { DefinitionObject } from "../services/promoted_attribute_definition_parser";
|
2025-11-22 22:25:32 +02:00
|
|
|
|
|
|
|
|
interface Cell {
|
|
|
|
|
definitionAttr: FAttribute;
|
2025-11-23 10:22:58 +02:00
|
|
|
definition: DefinitionObject;
|
2025-11-22 22:25:32 +02:00
|
|
|
valueAttr: Attribute;
|
|
|
|
|
valueName: string;
|
|
|
|
|
}
|
2025-11-22 22:10:51 +02:00
|
|
|
|
2025-11-23 10:46:56 +02:00
|
|
|
interface CellProps {
|
|
|
|
|
cell: Cell,
|
|
|
|
|
cells: Cell[],
|
|
|
|
|
shouldFocus: boolean;
|
|
|
|
|
setCells(cells: Cell[]): void;
|
|
|
|
|
setCellToFocus(cell: Cell): void;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-22 21:58:52 +02:00
|
|
|
export default function PromotedAttributes() {
|
2025-11-22 22:10:51 +02:00
|
|
|
const { note } = useNoteContext();
|
2025-11-22 22:25:32 +02:00
|
|
|
const [ cells, setCells ] = useState<Cell[]>();
|
2025-11-23 10:25:57 +02:00
|
|
|
const [ viewType ] = useNoteLabel(note, "viewType");
|
2025-11-23 10:46:56 +02:00
|
|
|
const [ cellToFocus, setCellToFocus ] = useState<Cell>();
|
2025-11-22 22:10:51 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-11-23 10:25:57 +02:00
|
|
|
if (!note || viewType === "table") {
|
|
|
|
|
setCells([]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-11-22 22:10:51 +02:00
|
|
|
const promotedDefAttrs = note.getPromotedDefinitionAttributes();
|
|
|
|
|
const ownedAttributes = note.getOwnedAttributes();
|
|
|
|
|
// attrs are not resorted if position changes after the initial load
|
|
|
|
|
// promoted attrs are sorted primarily by order of definitions, but with multi-valued promoted attrs
|
|
|
|
|
// the order of attributes is important as well
|
|
|
|
|
ownedAttributes.sort((a, b) => a.position - b.position);
|
|
|
|
|
|
2025-11-22 22:25:32 +02:00
|
|
|
const cells: Cell[] = [];
|
2025-11-22 22:10:51 +02:00
|
|
|
for (const definitionAttr of promotedDefAttrs) {
|
|
|
|
|
const valueType = definitionAttr.name.startsWith("label:") ? "label" : "relation";
|
|
|
|
|
const valueName = definitionAttr.name.substr(valueType.length + 1);
|
|
|
|
|
|
|
|
|
|
let valueAttrs = ownedAttributes.filter((el) => el.name === valueName && el.type === valueType) as Attribute[];
|
|
|
|
|
|
|
|
|
|
if (valueAttrs.length === 0) {
|
|
|
|
|
valueAttrs.push({
|
|
|
|
|
attributeId: "",
|
|
|
|
|
type: valueType,
|
|
|
|
|
name: valueName,
|
|
|
|
|
value: ""
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (definitionAttr.getDefinition().multiplicity === "single") {
|
|
|
|
|
valueAttrs = valueAttrs.slice(0, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-22 22:25:32 +02:00
|
|
|
for (const valueAttr of valueAttrs) {
|
2025-11-23 10:22:58 +02:00
|
|
|
const definition = definitionAttr.getDefinition();
|
|
|
|
|
cells.push({ definitionAttr, definition, valueAttr, valueName });
|
2025-11-22 22:25:32 +02:00
|
|
|
}
|
2025-11-22 22:10:51 +02:00
|
|
|
}
|
|
|
|
|
setCells(cells);
|
2025-11-23 10:25:57 +02:00
|
|
|
}, [ note, viewType ]);
|
2025-11-22 22:10:51 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="promoted-attributes-widget">
|
|
|
|
|
<div className="promoted-attributes-container">
|
2025-11-23 10:46:56 +02:00
|
|
|
{cells?.map(cell => <PromotedAttributeCell cell={cell} cells={cells} setCells={setCells} shouldFocus={cell === cellToFocus} setCellToFocus={setCellToFocus} />)}
|
2025-11-22 22:10:51 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2025-11-22 21:58:52 +02:00
|
|
|
}
|
2025-11-22 22:25:32 +02:00
|
|
|
|
2025-11-23 10:46:56 +02:00
|
|
|
function PromotedAttributeCell(props: CellProps) {
|
|
|
|
|
const { valueName, valueAttr, definition, definitionAttr } = props.cell;
|
|
|
|
|
const inputId = useUniqueName(`value-${valueAttr.name}`);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!props.shouldFocus) return;
|
|
|
|
|
const inputEl = document.getElementById(inputId);
|
|
|
|
|
if (inputEl) {
|
|
|
|
|
inputEl.focus();
|
|
|
|
|
}
|
|
|
|
|
}, [ props.shouldFocus ]);
|
2025-11-22 22:25:32 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="promoted-attribute-cell">
|
|
|
|
|
<label for={inputId}>{definition.promotedAlias ?? valueName}</label>
|
2025-11-23 10:22:58 +02:00
|
|
|
<div className="input-group">
|
|
|
|
|
<input
|
|
|
|
|
tabIndex={200 + definitionAttr.position}
|
|
|
|
|
id={inputId}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2025-11-23 10:46:56 +02:00
|
|
|
<ActionCell />
|
|
|
|
|
<MultiplicityCell {...props} />
|
2025-11-23 10:22:58 +02:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 10:46:56 +02:00
|
|
|
function ActionCell() {
|
2025-11-23 10:22:58 +02:00
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
|
2025-11-22 22:25:32 +02:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
2025-11-23 10:22:58 +02:00
|
|
|
|
2025-11-23 10:46:56 +02:00
|
|
|
function MultiplicityCell({ cell, cells, setCells, setCellToFocus }: CellProps) {
|
2025-11-23 10:22:58 +02:00
|
|
|
return (cell.definition.multiplicity === "multi" &&
|
|
|
|
|
<td className="multiplicity">
|
2025-11-23 10:46:56 +02:00
|
|
|
<PromotedActionButton
|
|
|
|
|
icon="bx bx-plus"
|
|
|
|
|
title={t("promoted_attributes.add_new_attribute")}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
const index = cells.indexOf(cell);
|
|
|
|
|
const newCell: Cell = {
|
|
|
|
|
...cell,
|
|
|
|
|
valueAttr: {
|
|
|
|
|
attributeId: "",
|
|
|
|
|
type: cell.valueAttr.type,
|
|
|
|
|
name: cell.valueName,
|
|
|
|
|
value: ""
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
setCells([
|
|
|
|
|
...cells.slice(0, index + 1),
|
|
|
|
|
newCell,
|
|
|
|
|
...cells.slice(index + 1)
|
|
|
|
|
]);
|
|
|
|
|
setCellToFocus(newCell);
|
|
|
|
|
}}
|
|
|
|
|
/>{' '}
|
|
|
|
|
<PromotedActionButton
|
|
|
|
|
icon="bx bx-trash"
|
|
|
|
|
title={t("promoted_attributes.remove_this_attribute")}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2025-11-23 10:22:58 +02:00
|
|
|
</td>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 10:46:56 +02:00
|
|
|
function PromotedActionButton({ icon, title, onClick }: {
|
2025-11-23 10:22:58 +02:00
|
|
|
icon: string,
|
2025-11-23 10:46:56 +02:00
|
|
|
title: string,
|
|
|
|
|
onClick: () => void
|
|
|
|
|
})
|
2025-11-23 10:22:58 +02:00
|
|
|
{
|
|
|
|
|
return (
|
|
|
|
|
<span
|
|
|
|
|
className={clsx("tn-tool-button pointer", icon)}
|
|
|
|
|
title={title}
|
2025-11-23 10:46:56 +02:00
|
|
|
onClick={onClick}
|
2025-11-23 10:22:58 +02:00
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|