import * as React from "react"; import { CommonSelection, CommonValue, commonElement } from "./common"; import type { ElementOperation } from "./ElementOperation"; import { css } from "@emotion/css"; /** * なぜ `Selection | undefined` にしないかというと * 選択されていないといういみで `undefined` が渡ってくるため */ export type SumSelection = { tag: "content"; selection: CommonSelection | undefined; }; export type SumValue = { readonly tagList: ReadonlyArray; readonly index: number; readonly value: CommonValue | undefined; }; export type SumTagItem = { name: string; onSelect: () => void; }; const SumSelectionView: ElementOperation< SumSelection, SumValue >["selectionView"] = React.memo((props) => { const onChangeSelection = props.onChangeSelection; const onFocusContent = React.useCallback( (event: React.FocusEvent) => { event.preventDefault(); event.stopPropagation(); onChangeSelection({ tag: "content", selection: undefined, }); }, [onChangeSelection] ); const onChangeContentSelection = React.useCallback( (commonSelection: CommonSelection): void => { onChangeSelection({ tag: "content", selection: commonSelection, }); }, [onChangeSelection] ); return (
{props.value.tagList.map((value, index) => ( ))}
{props.value.value === undefined ? ( <> ) : (
)}
); }); SumSelectionView.displayName = "SumSelectionView"; const TagItem: React.VFC<{ index: number; isSelect: boolean; sumTagItem: SumTagItem; tagNameCount: number; }> = React.memo(({ index, isSelect, sumTagItem, tagNameCount }) => { return (
{sumTagItem.name}
); }); TagItem.displayName = "TagItem"; const tagCountToGridTemplateColumns = (tagCount: number): string => { return new Array(tagCountToColumnCount(tagCount)) .fill("1fr") .join(" "); }; const tagCountToColumnCount = (tagCount: number): number => { if (tagCount <= 1) { return 1; } if (tagCount === 2) { return 2; } if (tagCount === 3 || tagCount === 5 || tagCount === 6 || tagCount === 9) { return 3; } return 4; }; const tagCountAndIndexToGridArea = ( tagCount: number, index: number ): string => { const columnCount = tagCountToColumnCount(tagCount); const x = (index % columnCount) + 1; const y = Math.floor(index / columnCount) + 1; return `${y} / ${x} / ${y + 1} / ${x + 1}`; }; const SumDetailView: ElementOperation["detailView"] = React.memo((props) => { if (props.selection === undefined) { return (
sum( {props.value.tagList.map((item) => item.name).join(",")})
); } if (props.value.value === undefined) { return
sum の選択している値がない
; } return ( ); }); SumDetailView.displayName = "SumDetailView"; export const sumOperation: ElementOperation = { moveUp: (selection, value) => { if (selection.selection === undefined || value.value === undefined) { return selection; } return { tag: "content", selection: commonElement.moveUp(selection.selection, value.value), }; }, moveDown: (selection, value) => { if (selection.selection === undefined || value.value === undefined) { return selection; } return { tag: "content", selection: commonElement.moveDown(selection.selection, value.value), }; }, moveFirstChild: (selection, value) => { // 自身を選択してる場合 if (selection === undefined) { if (value.value !== undefined) { return { tag: "content", selection: undefined, }; } return undefined; } // 子要素を選択してる場合 if (value.value === undefined) { return undefined; } return { tag: "content", selection: commonElement.moveFirstChild(selection.selection, value.value), }; }, moveParent: (selection, value) => { if (selection.selection === undefined || value.value === undefined) { return undefined; } return { tag: "content", selection: commonElement.moveParent(selection.selection, value.value), }; }, selectionView: SumSelectionView, detailView: SumDetailView, };