Skip to content

Commit afa48b6

Browse files
committed
✨(frontend) create page from slash menu
We are now able to create a new page from the slash menu.
1 parent f12d30c commit afa48b6

File tree

7 files changed

+141
-16
lines changed

7 files changed

+141
-16
lines changed

src/frontend/apps/e2e/__tests__/app-impress/doc-create.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ test.describe('Doc Create', () => {
2929
await expect(page.getByTestId('grid-loader')).toBeHidden();
3030
await expect(docsGrid.getByText(docTitle)).toBeVisible();
3131
});
32+
33+
test('it creates a sub doc from slash menu editor', async ({
34+
page,
35+
browserName,
36+
}) => {
37+
const [title] = await createDoc(page, 'my-new-slash-doc', browserName, 1);
38+
39+
await verifyDocName(page, title);
40+
41+
await page.locator('.bn-block-outer').last().fill('/');
42+
await page
43+
.getByText('New sub-doc', {
44+
exact: true,
45+
})
46+
.click();
47+
48+
const input = page.getByRole('textbox', { name: 'doc title input' });
49+
await expect(input).toHaveText('');
50+
await expect(
51+
page.locator('.c__tree-view--row-content').getByText('Untitled document'),
52+
).toBeVisible();
53+
});
3254
});
3355

3456
test.describe('Doc Create: Not logged', () => {

src/frontend/apps/e2e/__tests__/app-impress/doc-routing.spec.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,11 @@ test.describe('Doc Routing', () => {
1616
await page.goto('/');
1717
});
1818

19-
test('Check the presence of the meta tag noindex', async ({ page }) => {
20-
const buttonCreateHomepage = page.getByRole('button', {
21-
name: 'New doc',
22-
});
23-
24-
await expect(buttonCreateHomepage).toBeVisible();
25-
await buttonCreateHomepage.click();
26-
await expect(
27-
page.getByRole('button', {
28-
name: 'Share',
29-
}),
30-
).toBeVisible();
19+
test('Check the presence of the meta tag noindex', async ({
20+
page,
21+
browserName,
22+
}) => {
23+
await createDoc(page, 'doc-routing-test', browserName, 1);
3124
const metaDescription = page.locator('meta[name="robots"]');
3225
await expect(metaDescription).toHaveAttribute('content', 'noindex');
3326
});
Lines changed: 30 additions & 0 deletions
Loading

src/frontend/apps/impress/src/features/docs/doc-editor/components/BlockNoteSuggestionMenu.tsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,49 @@ import {
99
import React, { useMemo } from 'react';
1010
import { useTranslation } from 'react-i18next';
1111

12-
import { DocsBlockSchema } from '../types';
12+
import {
13+
DocsBlockSchema,
14+
DocsInlineContentSchema,
15+
DocsStyleSchema,
16+
} from '../types';
1317

1418
import {
1519
getCalloutReactSlashMenuItems,
1620
getDividerReactSlashMenuItems,
1721
} from './custom-blocks';
22+
import { useGetInterlinkingMenuItems } from './custom-inline-content';
1823
import XLMultiColumn from './xl-multi-column';
1924

2025
const getMultiColumnSlashMenuItems =
2126
XLMultiColumn?.getMultiColumnSlashMenuItems;
2227

2328
export const BlockNoteSuggestionMenu = () => {
24-
const editor = useBlockNoteEditor<DocsBlockSchema>();
29+
const editor = useBlockNoteEditor<
30+
DocsBlockSchema,
31+
DocsInlineContentSchema,
32+
DocsStyleSchema
33+
>();
2534
const { t } = useTranslation();
2635
const basicBlocksName = useDictionary().slash_menu.page_break.group;
36+
const getInterlinkingMenuItems = useGetInterlinkingMenuItems();
2737

2838
const getSlashMenuItems = useMemo(() => {
39+
// We insert it after the "Code Block" item to have the interlinking block displayed after the basic blocks
40+
const defaultMenu = getDefaultReactSlashMenuItems(editor);
41+
const index = defaultMenu.findIndex(
42+
(item) => item.aliases?.includes('code') && item.aliases?.includes('pre'),
43+
);
44+
const newSlashMenuItems = [
45+
...defaultMenu.slice(0, index + 1),
46+
...getInterlinkingMenuItems(t),
47+
...defaultMenu.slice(index + 1),
48+
];
49+
2950
return async (query: string) =>
3051
Promise.resolve(
3152
filterSuggestionItems(
3253
combineByGroup(
33-
getDefaultReactSlashMenuItems(editor),
54+
newSlashMenuItems,
3455
getCalloutReactSlashMenuItems(editor, t, basicBlocksName),
3556
getMultiColumnSlashMenuItems?.(editor) || [],
3657
getPageBreakReactSlashMenuItems(editor),
@@ -39,7 +60,7 @@ export const BlockNoteSuggestionMenu = () => {
3960
query,
4061
),
4162
);
42-
}, [basicBlocksName, editor, t]);
63+
}, [basicBlocksName, editor, getInterlinkingMenuItems, t]);
4364

4465
return (
4566
<SuggestionMenuController
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { useTreeContext } from '@gouvfr-lasuite/ui-kit';
2+
import { TFunction } from 'i18next';
3+
import { useRouter } from 'next/navigation';
4+
import { useCallback } from 'react';
5+
6+
import AddPageIcon from '@/docs/doc-editor/assets/doc-plus.svg';
7+
import { Doc, useCreateChildDoc, useDocStore } from '@/docs/doc-management';
8+
9+
export const getInterlinkingMenuItems = (
10+
t: TFunction<'translation', undefined>,
11+
group: string,
12+
createPage: () => void,
13+
) => [
14+
{
15+
title: t('New sub-doc'),
16+
onItemClick: createPage,
17+
aliases: ['new sub-doc'],
18+
group,
19+
icon: <AddPageIcon />,
20+
subtext: t('Create a new sub-doc'),
21+
},
22+
];
23+
24+
export const useGetInterlinkingMenuItems = () => {
25+
const treeContext = useTreeContext<Doc>();
26+
const router = useRouter();
27+
const { currentDoc } = useDocStore();
28+
29+
const { mutate: createChildDoc } = useCreateChildDoc({
30+
onSuccess: (createdDoc) => {
31+
const newDoc = {
32+
...createdDoc,
33+
children: [],
34+
childrenCount: 0,
35+
parentId: currentDoc?.id ?? undefined,
36+
};
37+
treeContext?.treeData.addChild(currentDoc?.id || null, newDoc);
38+
39+
router.push(`/docs/${newDoc.id}`);
40+
treeContext?.treeData.setSelectedNode(createdDoc);
41+
},
42+
});
43+
44+
return useCallback(
45+
(t: TFunction<'translation', undefined>) =>
46+
getInterlinkingMenuItems(
47+
t,
48+
t('Links'),
49+
() =>
50+
currentDoc?.id &&
51+
createChildDoc({
52+
parentId: currentDoc.id,
53+
}),
54+
),
55+
[createChildDoc, currentDoc?.id],
56+
);
57+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './InterlinkingSearchInlineContent';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Interlinking';

0 commit comments

Comments
 (0)