Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ function TemplateEditor() {
component: CustomFieldList // Custom list component
}}

// Toolbar (optional)
toolbar={true} // Render built-in toolbar container
// toolbar="#my-toolbar" // Mount into existing element
// toolbar={{ // Configure built-in toolbar
// toolbarGroups: ['center'],
// excludeItems: ['italic', 'bold'],
// }}

// Event handlers
onReady={() => {}}
onTrigger={(event) => {}}
Expand Down
8 changes: 4 additions & 4 deletions demo/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ header {
}

.header-content {
max-width: 1400px;
max-width: 1600px;
margin: 0 auto;
display: flex;
justify-content: space-between;
Expand Down Expand Up @@ -66,7 +66,7 @@ header {

/* Container */
.container {
max-width: 1400px;
max-width: 1600px;
margin: 2rem auto;
padding: 0 2rem;
}
Expand Down Expand Up @@ -229,11 +229,11 @@ header {
gap: 0.5rem;
}

.superdoc-template-builder > div {
.superdoc-template-builder>div {
flex-direction: column !important;
}

.superdoc-field-list {
width: 100% !important;
}
}
}
1 change: 1 addition & 0 deletions demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export function App() {
document={documentConfig}
fields={fieldsConfig}
list={listConfig}
toolbar={true}
onReady={handleReady}
onTrigger={handleTrigger}
onFieldInsert={handleFieldInsert}
Expand Down
64 changes: 59 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
editor: Editor,
): Types.TemplateField[] => {
const structuredContentHelpers =
(editor.helpers as any)?.structuredContentCommands;

Check warning on line 22 in src/index.tsx

View workflow job for this annotation

GitHub Actions / validate

Unexpected any. Specify a different type

if (!structuredContentHelpers?.getStructuredContentTags) {
return [];
Expand All @@ -29,7 +29,7 @@
structuredContentHelpers.getStructuredContentTags(editor.state) || [];

return tags
.map((entry: any) => {

Check warning on line 32 in src/index.tsx

View workflow job for this annotation

GitHub Actions / validate

Unexpected any. Specify a different type
const node = entry?.node ?? entry;
const attrs = node?.attrs ?? {};

Expand Down Expand Up @@ -68,6 +68,35 @@
return true;
};

const resolveToolbar = (
toolbar: Types.SuperDocTemplateBuilderProps["toolbar"],
) => {
if (!toolbar) return null;

if (toolbar === true) {
return {
selector: "#superdoc-toolbar",
config: {} as Omit<Types.ToolbarConfig, "selector">,
renderDefaultContainer: true,
};
}

if (typeof toolbar === "string") {
return {
selector: toolbar,
config: {} as Omit<Types.ToolbarConfig, "selector">,
renderDefaultContainer: false,
};
}

const { selector, ...config } = toolbar;
return {
selector: selector || "#superdoc-toolbar",
config,
renderDefaultContainer: selector === undefined,
};
};

const SuperDocTemplateBuilder = forwardRef<
Types.SuperDocTemplateBuilderHandle,
Types.SuperDocTemplateBuilderProps
Expand All @@ -77,6 +106,7 @@
fields = {},
menu = {},
list = {},
toolbar,
onReady,
onTrigger,
onFieldInsert,
Expand Down Expand Up @@ -115,7 +145,7 @@

const trigger = menu.trigger || "{{";

const availableFields = fieldsRef.current.available || [];

Check warning on line 148 in src/index.tsx

View workflow job for this annotation

GitHub Actions / validate

The 'availableFields' logical expression could make the dependencies of useCallback Hook (at line 161) change on every render. Move it inside the useCallback callback. Alternatively, wrap the initialization of 'availableFields' in its own useMemo() Hook

const computeFilteredFields = useCallback(
(query: string) => {
Expand Down Expand Up @@ -336,7 +366,7 @@
const initSuperDoc = async () => {
const { SuperDoc } = await import("superdoc");

const instance = new SuperDoc({
const config: Record<string, unknown> = {
selector: containerRef.current!,
document: document?.source,
documentMode: document?.mode || "editing",
Expand All @@ -345,7 +375,7 @@
const editor = instance.activeEditor;

// Setup trigger detection
editor.on("update", ({ editor: e }: any) => {

Check warning on line 378 in src/index.tsx

View workflow job for this annotation

GitHub Actions / validate

Unexpected any. Specify a different type
const { state } = e;
const { from } = state.selection;

Expand Down Expand Up @@ -419,6 +449,21 @@

onReady?.();
},
};

const instance = new SuperDoc({
...config,
...(toolbarSettings && {
toolbar: toolbarSettings.selector,
modules: {
toolbar: {
selector: toolbarSettings.selector,
toolbarGroups: toolbarSettings.config.toolbarGroups || ["center"],
excludeItems: toolbarSettings.config.excludeItems || [],
...toolbarSettings.config,
},
},
}),
});

superdocRef.current = instance;
Expand All @@ -434,13 +479,14 @@
superdocRef.current = null;
}
};
}, [

Check warning on line 482 in src/index.tsx

View workflow job for this annotation

GitHub Actions / validate

React Hook useEffect has missing dependencies: 'resetMenuFilter', 'toolbarSettings', and 'updateMenuFilter'. Either include them or remove the dependency array
document?.source,
document?.mode,
trigger,
discoverFields,
onReady,
onTrigger,
toolbar,
]);

const handleMenuSelect = useCallback(
Expand Down Expand Up @@ -519,12 +565,11 @@

const exportTemplate = useCallback(
async (options?: { fileName?: string }): Promise<void> => {
const editor = superdocRef.current?.activeEditor;
if (!editor) return;

try {
await editor.exportDocx?.({
fileName: options?.fileName || "document.docx",
await superdocRef.current?.export({
exportType: ["docx"],
exportedName: options?.fileName ? options?.fileName : "document"
});
} catch (error) {
console.error("Failed to export DOCX", error);
Expand All @@ -551,6 +596,8 @@
const MenuComponent = menu.component || FieldMenu;
const ListComponent = list.component || FieldList;

const toolbarSettings = resolveToolbar(toolbar);

return (
<div
className={`superdoc-template-builder ${className || ""}`}
Expand All @@ -571,6 +618,13 @@

{/* Document */}
<div className="superdoc-template-builder-document" style={{ flex: 1 }}>
{toolbarSettings?.renderDefaultContainer && (
<div
id="superdoc-toolbar"
className="superdoc-template-builder-toolbar"
data-testid="template-builder-toolbar"
/>
)}
<div
ref={containerRef}
className="superdoc-template-builder-editor"
Expand Down
13 changes: 13 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
label: string;
category?: string;
defaultValue?: string;
metadata?: Record<string, any>;

Check warning on line 8 in src/types.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected any. Specify a different type
}

export interface TemplateField {
Expand Down Expand Up @@ -63,11 +63,24 @@
position?: "left" | "right";
}

export interface ToolbarConfig {
selector?: string;
toolbarGroups?: string[];
groups?: Record<string, string[]>;
fonts?: string[] | null;
hideButtons?: boolean;
responsiveToContainer?: boolean;
excludeItems?: string[];
texts?: Record<string, string>;
icons?: Record<string, any>;

Check warning on line 75 in src/types.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected any. Specify a different type
}

export interface SuperDocTemplateBuilderProps {
document?: DocumentConfig;
fields?: FieldsConfig;
menu?: MenuConfig;
list?: ListConfig;
toolbar?: boolean | string | ToolbarConfig;

// Events
onReady?: () => void;
Expand Down