expand subpages node functionality

This commit is contained in:
Salihu
2026-09-11 23:37:51 +01:00
parent 6205bbeb90
commit b1d3c84baa
7 changed files with 337 additions and 67 deletions
@@ -1,2 +1,2 @@
export { Subpages } from "./subpages";
export type { SubpagesAttributes, SubpagesOptions } from "./subpages";
export type { SubpagesAttributes, SubpagesOptions, SubpagesSortBy } from "./subpages";
@@ -6,12 +6,17 @@ export interface SubpagesOptions {
view: any;
}
export interface SubpagesAttributes {}
export type SubpagesSortBy = "default" | "title-asc" | "title-desc";
export interface SubpagesAttributes {
sortBy?: SubpagesSortBy;
}
declare module "@tiptap/core" {
interface Commands<ReturnType> {
subpages: {
insertSubpages: (attributes?: SubpagesAttributes) => ReturnType;
setSubpagesSortBy: (sortBy: SubpagesSortBy) => ReturnType;
};
}
}
@@ -26,6 +31,19 @@ export const Subpages = Node.create<SubpagesOptions>({
};
},
addAttributes() {
return {
sortBy: {
default: "default",
parseHTML: (element: HTMLElement) =>
element.getAttribute("data-sort-by") || "default",
renderHTML: (attributes: SubpagesAttributes) => ({
"data-sort-by": attributes.sortBy || "default",
}),
},
};
},
group: "block",
atom: true,
draggable: true,
@@ -60,6 +78,19 @@ export const Subpages = Node.create<SubpagesOptions>({
attrs: attributes,
});
},
setSubpagesSortBy:
(sortBy) =>
({ commands }) => {
if (
sortBy !== 'default' &&
sortBy !== 'title-asc' &&
sortBy !== 'title-desc'
) {
return;
}
return commands.updateAttributes(this.name, { sortBy });
},
};
},