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
60 changes: 29 additions & 31 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 @@ -28,18 +28,16 @@
const tags =
structuredContentHelpers.getStructuredContentTags(editor.state) || [];

return tags
.map((entry: any) => {
const node = entry?.node ?? entry;
const attrs = node?.attrs ?? {};

return {
id: attrs.id,
alias: attrs.alias || attrs.label || "",
tag: attrs.tag,
} as Types.TemplateField;
})
.filter((field: Types.TemplateField) => Boolean(field.id));
return tags.map((entry: any) => {

Check warning on line 31 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 ?? {};

return {
id: attrs.id,
alias: attrs.alias || attrs.label || "",
tag: attrs.tag,
} as Types.TemplateField;
});
};

const areTemplateFieldsEqual = (
Expand Down Expand Up @@ -143,7 +141,9 @@
const [templateFields, setTemplateFields] = useState<Types.TemplateField[]>(
fields.initial || [],
);
const [selectedFieldId, setSelectedFieldId] = useState<string | null>(null);
const [selectedFieldId, setSelectedFieldId] = useState<string | number | null>(
null,
);
const [menuVisible, setMenuVisible] = useState(false);
const [menuPosition, setMenuPosition] = useState<DOMRect | undefined>();
const [menuQuery, setMenuQuery] = useState<string>("");
Expand All @@ -165,7 +165,7 @@

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

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

Check warning on line 168 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 181) 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 @@ -202,13 +202,12 @@
if (!superdocRef.current?.activeEditor) return false;

const editor = superdocRef.current.activeEditor;
const fieldId = `field_${Date.now()}`;
const previousFields = templateFields;

const success =
mode === "inline"
? editor.commands.insertStructuredContentInline?.({
attrs: {
id: fieldId,
alias: field.alias,
tag: field.metadata
? JSON.stringify(field.metadata)
Expand All @@ -218,7 +217,6 @@
})
: editor.commands.insertStructuredContentBlock?.({
attrs: {
id: fieldId,
alias: field.alias,
tag: field.metadata
? JSON.stringify(field.metadata)
Expand All @@ -228,28 +226,28 @@
});

if (success) {
const newField: Types.TemplateField = {
id: fieldId,
alias: field.alias,
tag: field.category,
};
const updatedFields = getTemplateFieldsFromEditor(editor);

setTemplateFields((prev) => {
const updated = [...prev, newField];
onFieldsChange?.(updated);
return updated;
});
setTemplateFields(updatedFields);
onFieldsChange?.(updatedFields);

onFieldInsert?.(newField);
const insertedField = updatedFields.find(
(candidate) =>
!previousFields.some((existing) => existing.id === candidate.id),
);

if (insertedField) {
onFieldInsert?.(insertedField);
}
}

return success;
},
[onFieldInsert, onFieldsChange],
[onFieldInsert, onFieldsChange, templateFields],
);

const updateField = useCallback(
(id: string, updates: Partial<Types.TemplateField>): boolean => {
(id: string | number, updates: Partial<Types.TemplateField>): boolean => {
if (!superdocRef.current?.activeEditor) return false;

const editor = superdocRef.current.activeEditor;
Expand All @@ -275,7 +273,7 @@
);

const deleteField = useCallback(
(id: string): boolean => {
(id: string | number): boolean => {
const editor = superdocRef.current?.activeEditor;

if (!editor) {
Expand Down Expand Up @@ -348,7 +346,7 @@
);

const selectField = useCallback(
(id: string) => {
(id: string | number) => {
if (!superdocRef.current?.activeEditor) return;

const editor = superdocRef.current.activeEditor;
Expand Down Expand Up @@ -395,7 +393,7 @@
const editor = instance.activeEditor;

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

Check warning on line 396 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 @@ -507,7 +505,7 @@

superdocRef.current = null;
};
}, [

Check warning on line 508 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,
Expand Down
17 changes: 10 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
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 {
id: string;
id: string | number;
alias: string;
tag?: string;
position?: number;
Expand Down Expand Up @@ -38,8 +38,8 @@
export interface FieldListProps {
fields: TemplateField[];
onSelect: (field: TemplateField) => void;
onDelete: (fieldId: string) => void;
selectedFieldId?: string;
onDelete: (fieldId: string | number) => void;
selectedFieldId?: string | number;
}

export interface DocumentConfig {
Expand Down Expand Up @@ -72,7 +72,7 @@
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 {
Expand All @@ -87,7 +87,7 @@
onTrigger?: (event: TriggerEvent) => void;
onFieldInsert?: (field: TemplateField) => void;
onFieldUpdate?: (field: TemplateField) => void;
onFieldDelete?: (fieldId: string) => void;
onFieldDelete?: (fieldId: string | number) => void;
onFieldsChange?: (fields: TemplateField[]) => void;
onFieldSelect?: (field: TemplateField | null) => void;
onFieldCreate?: (
Expand All @@ -105,9 +105,12 @@
insertBlockField: (
field: Partial<FieldDefinition> & { alias: string },
) => boolean;
updateField: (id: string, updates: Partial<TemplateField>) => boolean;
deleteField: (id: string) => boolean;
selectField: (id: string) => void;
updateField: (
id: string | number,
updates: Partial<TemplateField>,
) => boolean;
deleteField: (id: string | number) => boolean;
selectField: (id: string | number) => void;
nextField: () => void;
previousField: () => void;
getFields: () => TemplateField[];
Expand Down