Skip to content
Merged
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,24 @@ const MARKDOWNTABLE_TO_HANDSONTABLE_ALIGNMENT_SYMBOL_MAPPING = {
'': '',
};

export const HandsontableModalSubstance = (): JSX.Element => {

const HandsontableModalSubstance = (): JSX.Element => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要な export の削除

const { t } = useTranslation('commons');
const handsontableModalData = useHandsontableModalStatus();
const { close: closeHandsontableModal } = useHandsontableModalActions();
const handsontableModalForEditorData = useHandsontableModalForEditorStatus();
const { close: closeHandsontableModalForEditor } = useHandsontableModalForEditorActions();

const isOpened = handsontableModalData?.isOpened ?? false;
const isOpendInEditor = handsontableModalForEditorData?.isOpened ?? false;
// for View
const { close: closeHandsontableModal } = useHandsontableModalActions();
const handsontableModalData = useHandsontableModalStatus();
const isOpened = handsontableModalData.isOpened;
const table = handsontableModalData?.table;
const autoFormatMarkdownTable = handsontableModalData?.autoFormatMarkdownTable ?? false;
const editor = handsontableModalForEditorData?.editor;
const onSave = handsontableModalData?.onSave;

// for Editor
const { close: closeHandsontableModalForEditor } = useHandsontableModalForEditorActions();
const handsontableModalForEditorData = useHandsontableModalForEditorStatus();
const isOpendInEditor = handsontableModalForEditorData.isOpened;
const editor = handsontableModalForEditorData?.editor;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • どの hook 由来の変数か追いにくかったため view で使うものと editor で使うものを分けた
  • .isOpened については jotai 化したタイミング nullable ではなくなったためオプショナルチェーンを外した



// Memoize default table creation
const defaultMarkdownTable = useMemo(() => {
return new MarkdownTable(
Expand Down Expand Up @@ -114,12 +117,18 @@ export const HandsontableModalSubstance = (): JSX.Element => {

// Memoize modal open handler
const handleModalOpen = useCallback(() => {
const markdownTableState = table == null && editor != null ? getMarkdownTable(editor) : table;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

バグの原因

エディターからモーダルを開く際に、関係のない "table" という変数を見ていたから。"table" は2回目にモーダルを開く時に必ず not null になる。

変数 table について

"table" は useHandsontableModalStatus 由来のもの

const handsontableModalData = useHandsontableModalStatus();
const isOpened = handsontableModalData.isOpened;
const table = handsontableModalData?.table;

modal を閉じる際に呼ばれる close メソッドで closeHandsontableModal() が呼ばれる

const cancel = () => {
closeHandsontableModal();
closeHandsontableModalForEditor();
setIsDataImportAreaExpanded(false);
setIsWindowExpanded(false);
};

closeHandsontableModal() によって useHandsontableModalStatus の table が defaultMarkdownTable になる

const defaultMarkdownTable = () => {
return new MarkdownTable(
[
['col1', 'col2', 'col3'],
['', '', ''],
['', '', ''],
],
{
align: ['', '', ''],
},
);
};
// Atom definition
const handsontableModalAtom = atom<HandsontableModalState>({
isOpened: false,
});
// Read-only hook
export const useHandsontableModalStatus = () => {
const [state] = useAtom(handsontableModalAtom);
return state;
};
// Actions-only hook
export const useHandsontableModalActions = () => {
const setState = useSetAtom(handsontableModalAtom);
const open = (
table: MarkdownTable,
autoFormatMarkdownTable?: boolean,
onSave?: HandsontableModalSaveHandler,
) => {
setState({
isOpened: true,
table,
autoFormatMarkdownTable,
onSave,
});
};
const close = () => {
setState({
isOpened: false,
table: defaultMarkdownTable(),
autoFormatMarkdownTable: false,
onSave: undefined,
});
};
return { open, close };
};

let markdownTableState: MarkdownTable | undefined;
if (isOpendInEditor) {
markdownTableState = editor != null ? getMarkdownTable(editor) : undefined;
}
else {
markdownTableState = table;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View で開く場合と Editor で開く場合で処理を分けた

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isOpenedInEditor じゃないかな (e が足りない)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正しました

const initTableInstance = markdownTableState == null ? defaultMarkdownTable : markdownTableState.clone();
setMarkdownTable(markdownTableState ?? defaultMarkdownTable);
setMarkdownTableOnInit(initTableInstance);
debouncedHandleWindowExpandedChange();
}, [table, editor, defaultMarkdownTable, debouncedHandleWindowExpandedChange]);
}, [isOpendInEditor, defaultMarkdownTable, debouncedHandleWindowExpandedChange, editor, table]);

// Memoize expand/contract handlers
const expandWindow = useCallback(() => {
Expand Down Expand Up @@ -536,5 +545,14 @@ export const HandsontableModalSubstance = (): JSX.Element => {
};

export const HandsontableModal = (): JSX.Element => {
const handsontableModalData = useHandsontableModalStatus();
const handsontableModalForEditorData = useHandsontableModalForEditorStatus();

const isOpened = (handsontableModalData.isOpened || handsontableModalForEditorData.isOpened);

if (!isOpened) {
return <></>;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dynamic import 後の HandsontableModalSubstance の不要な rerender 対策

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

今のままだとモーダルの fade エフェクトがなくなったりするという微妙なデメリットがある

ちゃんとやるなら他のモーダル (ShortcutsModal など) を参考に、一番外側の <Modal> は Container component (lightweight, always rendered) に含むように改修することになる

HandsontableModalSubstance は view / editor の場合分けなしに両対応するとして、
それをラップする HandsontableModal 側で場合分けすべきかなと思う

ちなみに DrawioModal も同じかもしれない

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ShortcutsModal を参考に修正しました。
View / Editor の場合分けは HandsontableModal で行い HandsontableModalSubstance では意識しなくて良いようにしました


return <HandsontableModalSubstance />;
};
Loading