feat(views/table): disable drag if sorted

This commit is contained in:
Elian Doran 2025-06-27 20:30:36 +03:00
parent 6a0b24f032
commit bb0f384a39
No known key found for this signature in database
2 changed files with 53 additions and 48 deletions

View File

@ -37,8 +37,7 @@ export function buildColumnDefinitions(info: PromotedAttributeInformation[]) {
}, },
{ {
field: "title", field: "title",
editable: true, editable: true
rowDrag: true,
}, },
{ {
field: "position" field: "position"

View File

@ -81,8 +81,8 @@ export default class TableView extends ViewMode<StateInfo> {
this.api = createGrid(el, { this.api = createGrid(el, {
...buildData(parentNote, info, notes), ...buildData(parentNote, info, notes),
...setupEditing(), ...this.setupEditing(),
...setupDragging(), ...this.setupDragging(),
initialState, initialState,
async onGridReady(event) { async onGridReady(event) {
applyHeaderCustomization(el, event.api); applyHeaderCustomization(el, event.api);
@ -101,56 +101,62 @@ export default class TableView extends ViewMode<StateInfo> {
}); });
} }
} private setupEditing(): GridOptions<TableData> {
return {
onCellValueChanged(event) {
if (event.type !== "cellValueChanged") {
return;
}
function setupEditing(): GridOptions<TableData> { const noteId = event.data.noteId;
return { const name = event.colDef.field;
onCellValueChanged(event) { if (!name) {
if (event.type !== "cellValueChanged") { return;
return; }
}
const noteId = event.data.noteId; const { newValue } = event;
const name = event.colDef.field; if (name === "title") {
if (!name) { // TODO: Deduplicate with note_title.
return; server.put(`notes/${noteId}/title`, { title: newValue });
} }
const { newValue } = event; if (name.startsWith("labels.")) {
if (name === "title") { const labelName = name.split(".", 2)[1];
// TODO: Deduplicate with note_title. setLabel(noteId, labelName, newValue);
server.put(`notes/${noteId}/title`, { title: newValue }); }
}
if (name.startsWith("labels.")) {
const labelName = name.split(".", 2)[1];
setLabel(noteId, labelName, newValue);
} }
} }
} }
}
function setupDragging() { private setupDragging() {
return { if (this.parentNote.hasLabel("sorted")) {
onRowDragEnd(e) { return {};
const fromIndex = e.node.rowIndex;
const toIndex = e.overNode?.rowIndex;
if (fromIndex === null || toIndex === null || toIndex === undefined || fromIndex === toIndex) {
return;
}
const isBelow = (toIndex > fromIndex);
const fromBranchId = e.node.data?.branchId;
const toBranchId = e.overNode?.data?.branchId;
if (fromBranchId === undefined || toBranchId === undefined) {
return;
}
if (isBelow) {
branches.moveAfterBranch([ fromBranchId ], toBranchId);
} else {
branches.moveBeforeBranch([ fromBranchId ], toBranchId);
}
} }
};
const config: GridOptions<TableData> = {
rowDragEntireRow: true,
onRowDragEnd(e) {
const fromIndex = e.node.rowIndex;
const toIndex = e.overNode?.rowIndex;
if (fromIndex === null || toIndex === null || toIndex === undefined || fromIndex === toIndex) {
return;
}
const isBelow = (toIndex > fromIndex);
const fromBranchId = e.node.data?.branchId;
const toBranchId = e.overNode?.data?.branchId;
if (fromBranchId === undefined || toBranchId === undefined) {
return;
}
if (isBelow) {
branches.moveAfterBranch([ fromBranchId ], toBranchId);
} else {
branches.moveBeforeBranch([ fromBranchId ], toBranchId);
}
}
};
return config;
}
} }