Skip to content
Merged
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
28 changes: 26 additions & 2 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 @@ -97,6 +97,26 @@
};
};

const MENU_VIEWPORT_PADDING = 10;
const MENU_APPROX_WIDTH = 250;
const MENU_APPROX_HEIGHT = 300;

const clampToViewport = (rect: DOMRect): DOMRect => {
const maxLeft = window.innerWidth - MENU_APPROX_WIDTH - MENU_VIEWPORT_PADDING;
const maxTop =
window.innerHeight - MENU_APPROX_HEIGHT - MENU_VIEWPORT_PADDING;

const clampedLeft = Math.min(rect.left, maxLeft);
const clampedTop = Math.min(rect.top, maxTop);

return new DOMRect(
Math.max(clampedLeft, MENU_VIEWPORT_PADDING),
Math.max(clampedTop, MENU_VIEWPORT_PADDING),
rect.width,
rect.height,
);
};

const SuperDocTemplateBuilder = forwardRef<
Types.SuperDocTemplateBuilderHandle,
Types.SuperDocTemplateBuilderProps
Expand Down Expand Up @@ -145,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 @@ -375,7 +395,7 @@
const editor = instance.activeEditor;

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

Check warning on line 398 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 All @@ -385,7 +405,9 @@

if (text === trigger) {
const coords = e.view.coordsAtPos(from);
const bounds = new DOMRect(coords.left, coords.top, 0, 0);
const bounds = clampToViewport(
new DOMRect(coords.left, coords.top, 0, 0),
);

const cleanup = () => {
const editor = superdocRef.current?.activeEditor;
Expand Down Expand Up @@ -435,7 +457,9 @@
updateMenuFilter(queryText);

const coords = e.view.coordsAtPos(from);
const bounds = new DOMRect(coords.left, coords.top, 0, 0);
const bounds = clampToViewport(
new DOMRect(coords.left, coords.top, 0, 0),
);
setMenuPosition(bounds);
});

Expand Down Expand Up @@ -479,7 +503,7 @@
superdocRef.current = null;
}
};
}, [

Check warning on line 506 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
Loading