-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andy Wilson
committed
May 2, 2024
1 parent
e8b6efd
commit 013623c
Showing
18 changed files
with
912 additions
and
4,585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import {useCallback, useMemo, useState} from 'react'; | ||
import * as React from 'react'; | ||
|
||
import Modal from '../ui/Modal'; | ||
|
||
export default function useModal(): [ | ||
JSX.Element | null, | ||
(title: string, showModal: (onClose: () => void) => React.JSX.Element) => void, | ||
] { | ||
const [modalContent, setModalContent] = useState<null | { | ||
closeOnClickOutside: boolean; | ||
content: JSX.Element; | ||
title: string; | ||
}>(null); | ||
|
||
const onClose = useCallback(() => { | ||
setModalContent(null); | ||
}, []); | ||
|
||
const modal = useMemo(() => { | ||
if (modalContent === null) { | ||
return null; | ||
} | ||
const {title, content, closeOnClickOutside} = modalContent; | ||
return ( | ||
<Modal | ||
onClose={onClose} | ||
title={title} | ||
closeOnClickOutside={closeOnClickOutside}> | ||
{content} | ||
</Modal> | ||
); | ||
}, [modalContent, onClose]); | ||
|
||
const showModal = useCallback( | ||
( | ||
title: string, | ||
// eslint-disable-next-line no-shadow | ||
getContent: (onClose: () => void) => JSX.Element, | ||
closeOnClickOutside = false, | ||
) => { | ||
setModalContent({ | ||
closeOnClickOutside, | ||
content: getContent(onClose), | ||
title, | ||
}); | ||
}, | ||
[onClose], | ||
); | ||
|
||
return [modal, showModal]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; | ||
import { | ||
$createTableNodeWithDimensions, | ||
INSERT_TABLE_COMMAND, | ||
} from '@lexical/table'; | ||
import { | ||
$insertNodes, | ||
COMMAND_PRIORITY_EDITOR, | ||
createCommand, | ||
EditorThemeClasses, | ||
Klass, | ||
LexicalCommand, | ||
LexicalEditor, | ||
LexicalNode, | ||
} from 'lexical'; | ||
import { | ||
createContext, | ||
PropsWithChildren, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
import * as React from 'react'; | ||
|
||
import Button from '../ui/Button'; | ||
import { DialogActions } from '../ui/Dialog'; | ||
import TextInput from '../ui/TextInput'; | ||
|
||
export type InsertTableCommandPayload = Readonly<{ | ||
columns: string; | ||
rows: string; | ||
includeHeaders?: boolean; | ||
}>; | ||
|
||
export type CellContextShape = { | ||
cellEditorConfig: null | CellEditorConfig; | ||
cellEditorPlugins: null | JSX.Element | Array<JSX.Element>; | ||
set: ( | ||
cellEditorConfig: null | CellEditorConfig, | ||
cellEditorPlugins: null | React.JSX.Element | Array<React.JSX.Element> | ||
) => void; | ||
}; | ||
|
||
export type CellEditorConfig = Readonly<{ | ||
namespace: string; | ||
nodes?: ReadonlyArray<Klass<LexicalNode>>; | ||
onError: (error: Error, editor: LexicalEditor) => void; | ||
readOnly?: boolean; | ||
theme?: EditorThemeClasses; | ||
}>; | ||
|
||
export const INSERT_NEW_TABLE_COMMAND: LexicalCommand<InsertTableCommandPayload> = | ||
createCommand('INSERT_NEW_TABLE_COMMAND'); | ||
|
||
export const CellContext = createContext<CellContextShape>({ | ||
cellEditorConfig: null, | ||
cellEditorPlugins: null, | ||
set: () => { | ||
// Empty | ||
}, | ||
}); | ||
|
||
export function TableContext({ children }: PropsWithChildren) { | ||
const [contextValue, setContextValue] = useState<{ | ||
cellEditorConfig: null | CellEditorConfig; | ||
cellEditorPlugins: null | React.JSX.Element | Array<React.JSX.Element>; | ||
}>({ | ||
cellEditorConfig: null, | ||
cellEditorPlugins: null, | ||
}); | ||
return ( | ||
<CellContext.Provider | ||
value={useMemo( | ||
() => ({ | ||
cellEditorConfig: contextValue.cellEditorConfig, | ||
cellEditorPlugins: contextValue.cellEditorPlugins, | ||
set: (cellEditorConfig, cellEditorPlugins) => { | ||
setContextValue({ cellEditorConfig, cellEditorPlugins }); | ||
}, | ||
}), | ||
[contextValue.cellEditorConfig, contextValue.cellEditorPlugins] | ||
)} | ||
> | ||
{children} | ||
</CellContext.Provider> | ||
); | ||
} | ||
|
||
export function InsertTableDialog({ | ||
activeEditor, | ||
onClose, | ||
}: { | ||
activeEditor: LexicalEditor; | ||
onClose: () => void; | ||
}): React.JSX.Element { | ||
const [rows, setRows] = useState('5'); | ||
const [columns, setColumns] = useState('5'); | ||
const [isDisabled, setIsDisabled] = useState(true); | ||
|
||
useEffect(() => { | ||
const row = Number(rows); | ||
const column = Number(columns); | ||
if (row && row > 0 && row <= 500 && column && column > 0 && column <= 50) { | ||
setIsDisabled(false); | ||
} else { | ||
setIsDisabled(true); | ||
} | ||
}, [rows, columns]); | ||
|
||
const onClick = () => { | ||
activeEditor.dispatchCommand(INSERT_TABLE_COMMAND, { | ||
columns, | ||
rows, | ||
}); | ||
|
||
onClose(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<TextInput | ||
placeholder={'# of rows (1-500)'} | ||
label='Rows' | ||
onChange={setRows} | ||
value={rows} | ||
data-test-id='table-modal-rows' | ||
type='number' | ||
/> | ||
<TextInput | ||
placeholder={'# of columns (1-50)'} | ||
label='Columns' | ||
onChange={setColumns} | ||
value={columns} | ||
data-test-id='table-modal-columns' | ||
type='number' | ||
/> | ||
<DialogActions data-test-id='table-model-confirm-insert'> | ||
<Button disabled={isDisabled} onClick={onClick}> | ||
Confirm | ||
</Button> | ||
</DialogActions> | ||
</> | ||
); | ||
} | ||
|
||
export function TablePlugin({ | ||
cellEditorConfig, | ||
children, | ||
}: { | ||
cellEditorConfig: CellEditorConfig; | ||
children: React.JSX.Element | Array<React.JSX.Element>; | ||
}): React.JSX.Element | null { | ||
const [editor] = useLexicalComposerContext(); | ||
const cellContext = useContext(CellContext); | ||
|
||
useEffect(() => { | ||
cellContext.set(cellEditorConfig, children); | ||
|
||
return editor.registerCommand<InsertTableCommandPayload>( | ||
INSERT_NEW_TABLE_COMMAND, | ||
({ columns, rows, includeHeaders }) => { | ||
const tableNode = $createTableNodeWithDimensions( | ||
Number(rows), | ||
Number(columns), | ||
includeHeaders | ||
); | ||
$insertNodes([tableNode]); | ||
return true; | ||
}, | ||
COMMAND_PRIORITY_EDITOR | ||
); | ||
}, [cellContext, cellEditorConfig, children, editor]); | ||
|
||
return null; | ||
} |
Oops, something went wrong.