mirror of
https://github.com/docmost/docmost.git
synced 2026-07-24 03:02:54 +10:00
89f13c3fbc
* feat(ee): bases Table and kanban UI, formula engine package, and the base-embed editor extension. * - default status - type fix - error helper * fix: base trash list handling * feat: base nodeview menu * feat: translation * fix number precision * feat(base): add focused-cell atom and cell coordinate types * feat(base): add cell focus-ring style * feat(base): add pure next-cell navigation helper * feat(base): keyboard navigation controller and grid wiring * update offerings * feat(base): cell focus ring, click-to-focus, and gridcell ARIA * feat(base): row ARIA index and selected state * feat(base): seed editor value on type-to-edit for free-text cells * feat(base): make column headers keyboard-focusable as tab stops * fix(base): remove focus outline on grid container * fix(base): show cell focus ring only while the grid is focused * feat(base): keyboard-navigate the row-number column for selection * fix(base): sync header/body horizontal scroll on header focus; expand row via Space, drop expander from tab order * fix(base): tab from long-text editor moves to next cell instead of leaving the table * fix(base): close view popovers on Escape regardless of focus; drop redundant property switch tab stop * fix(base): show cell focus ring only while the grid body itself is focused * fix(base): render view-tab rename as an inline pill so the tab band height stays put * fix(base): refer to the feature as 'base' rather than 'database' * fix: change permissions object shape * license file * fix tsconfig * fix base cache * fix: preserve sidebar title/icon on partial page updates * fix: skip duplicate row fetch when opening new kanban card * fix refetch * fix focus * fix spacing * fix(base): select grid cell on mousedown to avoid stale focus ring flash The focus ring is gated on the grid having DOM focus (.bodyGrid:focus .cellFocused), but the focusedCell atom is never cleared when the grid blurs. Clicking outside hides the ring via the :focus gate while the atom still points at the old cell. Selection was committed on click (mouseup), while the grid receives focus on mousedown. Clicking a new cell re-focused the grid before the atom updated, briefly painting the ring on the previously selected cell. Commit selection on mousedown so the atom updates in the same event that grants focus, before the browser paints. * fix: activate New row button via keyboard (Enter/Space) The New row control is a role=button div with no keydown handler, so Enter/Space never triggered it. It also lives inside the grid element, whose native keydown listener caught the Enter and ran cell navigation against the previously focused cell. Add Enter/Space activation to the button, and make the grid keyboard handler ignore keydowns that originate from a focusable child rather than the grid element itself, so in-grid controls handle their own keys. * fix(base): keep add-property popover within viewport on mobile Opened from the row detail modal, the create-property popover anchors to the bottom Add property button and flips upward on small screens, clipping its top (name field, formula editor) off-screen with no way to scroll to it. Bound the dropdown to the available height with the floating-ui size middleware and give it an internal scroll container. Disable react-remove-scroll isolation on the modal so the body-portaled popover can scroll on touch while the modal scroll lock stays active. * fix(base): enable grid cell editing on touch devices Cells could only enter edit mode via double-click or a physical keyboard, so touch devices had no way to edit a cell. Treat a touch/pen tap as the edit gesture, distinguishing a tap from a scroll by movement and branching per pointer type so mouse double-click stays unchanged. Also reveal the row expand button on hover-less devices so the row detail view stays reachable. * feat(editor): add base and kanban inserts to the toolbar * feat(base): insert row below via Shift+Enter on the primary cell * fix(base): place caret at end instead of selecting all when editing cells * fix(base): prevent popover inputs from losing focus on mobile in row detail modal * fix grid cells on mobile * sync * fix: read-only export * feat(base): add prefixed nanoid id schemas and generators * feat(base): enforce strict property/choice id validation * feat(base): make property id varchar with per-base composite pk * feat(base): pass property id as text to cell extractors * feat(base): scope property lookups per base and generate property ids in repo * feat(base): generate status template choice ids as nanoid * feat(base): generate choice ids as nanoid on the client * chore(base): seed choice ids with nanoid * fix(base): mint kanban choice ids as nanoid * sync * sync * sync
259 lines
8.3 KiB
TypeScript
259 lines
8.3 KiB
TypeScript
import { useState, useRef, useEffect, useCallback, useMemo } from "react";
|
|
import { TextInput } from "@mantine/core";
|
|
import { IconX } from "@tabler/icons-react";
|
|
import clsx from "clsx";
|
|
import {
|
|
IBaseProperty,
|
|
SelectTypeOptions,
|
|
Choice,
|
|
} from "@/ee/base/types/base.types";
|
|
import { choiceColor } from "@/ee/base/components/cells/choice-color";
|
|
import { useUpdatePropertyMutation } from "@/ee/base/queries/base-property-query";
|
|
import { generateBaseChoiceId } from "@/ee/base/utils/generate-base-id";
|
|
import { useListKeyboardNav } from "@/ee/base/hooks/use-list-keyboard-nav";
|
|
import cellClasses from "@/ee/base/styles/cells.module.css";
|
|
|
|
const CHOICE_COLORS = [
|
|
"gray", "red", "pink", "grape", "violet", "indigo",
|
|
"blue", "cyan", "teal", "green", "lime", "yellow", "orange",
|
|
];
|
|
|
|
const STATUS_CATEGORY_LABELS: Record<string, string> = {
|
|
todo: "To Do",
|
|
inProgress: "In Progress",
|
|
complete: "Complete",
|
|
};
|
|
const STATUS_CATEGORY_ORDER = ["todo", "inProgress", "complete"];
|
|
|
|
type NavItem =
|
|
| { kind: "choice"; choice: Choice }
|
|
| { kind: "add" };
|
|
|
|
type ChoiceGroup = { label: string | null; choices: Choice[] };
|
|
|
|
type ChoicePickerProps = {
|
|
property: IBaseProperty;
|
|
selectedIds: string[];
|
|
/** Multi keeps the picker open, hides picked options from the list and
|
|
* shows them as removable tags instead. */
|
|
multiple?: boolean;
|
|
/** Group options under status category headings. */
|
|
grouped?: boolean;
|
|
/** Offer "Add option: <search>" when the search has no exact match. */
|
|
allowCreate?: boolean;
|
|
onToggle: (choice: Choice) => void;
|
|
onEscape: () => void;
|
|
};
|
|
|
|
/** Searchable choice list shared by select-like editors (modal fields; the
|
|
* grid cells render the same UI and can migrate here). */
|
|
export function ChoicePicker({
|
|
property,
|
|
selectedIds,
|
|
multiple = false,
|
|
grouped = false,
|
|
allowCreate = false,
|
|
onToggle,
|
|
onEscape,
|
|
}: ChoicePickerProps) {
|
|
const typeOptions = property.typeOptions as SelectTypeOptions | undefined;
|
|
const choices = typeOptions?.choices ?? [];
|
|
const selectedSet = useMemo(() => new Set(selectedIds), [selectedIds]);
|
|
const selectedChoices = choices.filter((c) => selectedSet.has(c.id));
|
|
|
|
const [search, setSearch] = useState("");
|
|
const searchRef = useRef<HTMLInputElement>(null);
|
|
|
|
useEffect(() => {
|
|
requestAnimationFrame(() => searchRef.current?.focus());
|
|
}, []);
|
|
|
|
const groups = useMemo<ChoiceGroup[]>(() => {
|
|
const filtered = (
|
|
search
|
|
? choices.filter((c) =>
|
|
c.name.toLowerCase().includes(search.toLowerCase()),
|
|
)
|
|
: choices
|
|
).filter((c) => !multiple || !selectedSet.has(c.id));
|
|
|
|
if (!grouped) return [{ label: null, choices: filtered }];
|
|
|
|
const byCategory: Record<string, Choice[]> = {};
|
|
for (const choice of filtered) {
|
|
const cat = choice.category ?? "todo";
|
|
(byCategory[cat] ??= []).push(choice);
|
|
}
|
|
return STATUS_CATEGORY_ORDER.filter((key) => byCategory[key]?.length).map(
|
|
(key) => ({ label: STATUS_CATEGORY_LABELS[key] ?? key, choices: byCategory[key] }),
|
|
);
|
|
}, [choices, search, grouped, multiple, selectedSet]);
|
|
|
|
const flatChoices = useMemo(() => groups.flatMap((g) => g.choices), [groups]);
|
|
const choiceIdxMap = useMemo(() => {
|
|
const m = new Map<string, number>();
|
|
flatChoices.forEach((c, i) => m.set(c.id, i));
|
|
return m;
|
|
}, [flatChoices]);
|
|
|
|
const updatePropertyMutation = useUpdatePropertyMutation();
|
|
const trimmedSearch = search.trim();
|
|
const hasExactMatch = useMemo(
|
|
() =>
|
|
trimmedSearch.length > 0 &&
|
|
choices.some((c) => c.name.toLowerCase() === trimmedSearch.toLowerCase()),
|
|
[choices, trimmedSearch],
|
|
);
|
|
const showAddOption = allowCreate && trimmedSearch.length > 0 && !hasExactMatch;
|
|
const addOptionColor = useMemo(
|
|
() => CHOICE_COLORS[choices.length % CHOICE_COLORS.length],
|
|
[choices.length],
|
|
);
|
|
|
|
const navItems = useMemo<NavItem[]>(
|
|
() => [
|
|
...flatChoices.map((c) => ({ kind: "choice" as const, choice: c })),
|
|
...(showAddOption ? [{ kind: "add" as const }] : []),
|
|
],
|
|
[flatChoices, showAddOption],
|
|
);
|
|
|
|
const { activeIndex, setActiveIndex, handleNavKey, setOptionRef } =
|
|
useListKeyboardNav(navItems.length, [search, showAddOption]);
|
|
|
|
const handleAddOption = useCallback(() => {
|
|
if (!trimmedSearch) return;
|
|
const newChoice: Choice = {
|
|
id: generateBaseChoiceId(),
|
|
name: trimmedSearch,
|
|
color: addOptionColor,
|
|
};
|
|
const newChoices = [...choices, newChoice];
|
|
updatePropertyMutation.mutate({
|
|
propertyId: property.id,
|
|
pageId: property.pageId,
|
|
typeOptions: {
|
|
...typeOptions,
|
|
choices: newChoices,
|
|
choiceOrder: newChoices.map((c) => c.id),
|
|
},
|
|
});
|
|
onToggle(newChoice);
|
|
setSearch("");
|
|
}, [trimmedSearch, addOptionColor, choices, typeOptions, property, updatePropertyMutation, onToggle]);
|
|
|
|
const handleKeyDown = useCallback(
|
|
(e: React.KeyboardEvent) => {
|
|
if (e.key === "Escape") {
|
|
e.preventDefault();
|
|
onEscape();
|
|
return;
|
|
}
|
|
if (handleNavKey(e)) return;
|
|
if (e.key === "Enter") {
|
|
if (activeIndex >= 0 && activeIndex < navItems.length) {
|
|
e.preventDefault();
|
|
const item = navItems[activeIndex];
|
|
if (item.kind === "choice") onToggle(item.choice);
|
|
else handleAddOption();
|
|
return;
|
|
}
|
|
if (showAddOption) {
|
|
e.preventDefault();
|
|
handleAddOption();
|
|
}
|
|
}
|
|
},
|
|
[onEscape, handleNavKey, activeIndex, navItems, onToggle, handleAddOption, showAddOption],
|
|
);
|
|
|
|
const addOptionIdx = flatChoices.length;
|
|
|
|
return (
|
|
<>
|
|
{multiple && selectedChoices.length > 0 && (
|
|
<div className={cellClasses.personTagArea}>
|
|
{selectedChoices.map((choice) => (
|
|
<span
|
|
key={choice.id}
|
|
className={cellClasses.badge}
|
|
style={choiceColor(choice.color)}
|
|
>
|
|
{choice.name}
|
|
<button
|
|
type="button"
|
|
className={`${cellClasses.personTagRemove} ${cellClasses.badgeRemoveBtn}`}
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
onToggle(choice);
|
|
}}
|
|
>
|
|
<IconX size={10} />
|
|
</button>
|
|
</span>
|
|
))}
|
|
</div>
|
|
)}
|
|
<TextInput
|
|
ref={searchRef}
|
|
size="xs"
|
|
placeholder="Search..."
|
|
value={search}
|
|
onChange={(e) => setSearch(e.currentTarget.value)}
|
|
onKeyDown={handleKeyDown}
|
|
mb={4}
|
|
data-autofocus
|
|
/>
|
|
<div className={cellClasses.selectDropdown}>
|
|
{groups.map((group) => (
|
|
<div key={group.label ?? "all"}>
|
|
{group.label && (
|
|
<div className={cellClasses.selectCategoryLabel}>{group.label}</div>
|
|
)}
|
|
{group.choices.map((choice) => {
|
|
const idx = choiceIdxMap.get(choice.id) ?? -1;
|
|
const isSelected = !multiple && selectedSet.has(choice.id);
|
|
return (
|
|
<div
|
|
key={choice.id}
|
|
ref={setOptionRef(idx)}
|
|
className={clsx(
|
|
cellClasses.selectOption,
|
|
isSelected && cellClasses.selectOptionActive,
|
|
idx === activeIndex && cellClasses.selectOptionKeyboardActive,
|
|
)}
|
|
onMouseEnter={() => setActiveIndex(idx)}
|
|
onClick={() => onToggle(choice)}
|
|
>
|
|
<span
|
|
className={cellClasses.badge}
|
|
style={choiceColor(choice.color)}
|
|
>
|
|
{choice.name}
|
|
</span>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
))}
|
|
{showAddOption && (
|
|
<div
|
|
ref={setOptionRef(addOptionIdx)}
|
|
className={clsx(
|
|
cellClasses.addOptionRow,
|
|
addOptionIdx === activeIndex && cellClasses.selectOptionKeyboardActive,
|
|
)}
|
|
onMouseEnter={() => setActiveIndex(addOptionIdx)}
|
|
onClick={handleAddOption}
|
|
>
|
|
<span className={cellClasses.addOptionLabel}>Add option:</span>
|
|
<span className={cellClasses.badge} style={choiceColor(addOptionColor)}>
|
|
{trimmedSearch}
|
|
</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</>
|
|
);
|
|
}
|