mirror of
https://github.com/zadam/trilium.git
synced 2025-11-07 23:18:59 +01:00
refactor(collections/board): use same title editor for new columns
This commit is contained in:
parent
ede4b99bcd
commit
0d275b3259
@ -43,6 +43,27 @@ export default class BoardApi {
|
|||||||
await attributes.setLabel(noteId, this.statusAttribute, newColumn);
|
await attributes.setLabel(noteId, this.statusAttribute, newColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async addNewColumn(columnName: string) {
|
||||||
|
if (!columnName.trim()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.viewConfig) {
|
||||||
|
this.viewConfig = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.viewConfig.columns) {
|
||||||
|
this.viewConfig.columns = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the new column to persisted data if it doesn't exist
|
||||||
|
const existingColumn = this.viewConfig.columns.find(col => col.value === columnName);
|
||||||
|
if (!existingColumn) {
|
||||||
|
this.viewConfig.columns.push({ value: columnName });
|
||||||
|
this.saveConfig(this.viewConfig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async removeColumn(column: string) {
|
async removeColumn(column: string) {
|
||||||
// Remove the value from the notes.
|
// Remove the value from the notes.
|
||||||
const noteIds = this.byColumn?.get(column)?.map(item => item.note.noteId) || [];
|
const noteIds = this.byColumn?.get(column)?.map(item => item.note.noteId) || [];
|
||||||
|
|||||||
@ -189,46 +189,20 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
|
|||||||
<div className="column-drop-placeholder show" />
|
<div className="column-drop-placeholder show" />
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<AddNewColumn viewConfig={viewConfig} saveConfig={saveConfig} />
|
<AddNewColumn api={api} />
|
||||||
</div>
|
</div>
|
||||||
</BoardViewContext.Provider>
|
</BoardViewContext.Provider>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function AddNewColumn({ viewConfig, saveConfig }: { viewConfig?: BoardViewData, saveConfig: (data: BoardViewData) => void }) {
|
function AddNewColumn({ api }: { api: BoardApi }) {
|
||||||
const [ isCreatingNewColumn, setIsCreatingNewColumn ] = useState(false);
|
const [ isCreatingNewColumn, setIsCreatingNewColumn ] = useState(false);
|
||||||
const columnNameRef = useRef<HTMLInputElement>(null);
|
|
||||||
|
|
||||||
const addColumnCallback = useCallback(() => {
|
const addColumnCallback = useCallback(() => {
|
||||||
setIsCreatingNewColumn(true);
|
setIsCreatingNewColumn(true);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const finishEdit = useCallback((save: boolean) => {
|
|
||||||
const columnName = columnNameRef.current?.value;
|
|
||||||
if (!columnName || !save) {
|
|
||||||
setIsCreatingNewColumn(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the new column to persisted data if it doesn't exist
|
|
||||||
if (!viewConfig) {
|
|
||||||
viewConfig = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!viewConfig.columns) {
|
|
||||||
viewConfig.columns = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const existingColumn = viewConfig.columns.find(col => col.value === columnName);
|
|
||||||
if (!existingColumn) {
|
|
||||||
viewConfig.columns.push({ value: columnName });
|
|
||||||
saveConfig(viewConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsCreatingNewColumn(false);
|
|
||||||
}, [ viewConfig, saveConfig ]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`board-add-column ${isCreatingNewColumn ? "editing" : ""}`} onClick={addColumnCallback}>
|
<div className={`board-add-column ${isCreatingNewColumn ? "editing" : ""}`} onClick={addColumnCallback}>
|
||||||
{!isCreatingNewColumn
|
{!isCreatingNewColumn
|
||||||
@ -236,33 +210,25 @@ function AddNewColumn({ viewConfig, saveConfig }: { viewConfig?: BoardViewData,
|
|||||||
<Icon icon="bx bx-plus" />{" "}
|
<Icon icon="bx bx-plus" />{" "}
|
||||||
{t("board_view.add-column")}
|
{t("board_view.add-column")}
|
||||||
</>
|
</>
|
||||||
: <>
|
: (
|
||||||
<FormTextBox
|
<TitleEditor
|
||||||
inputRef={columnNameRef}
|
|
||||||
type="text"
|
|
||||||
placeholder={t("board_view.add-column-placeholder")}
|
placeholder={t("board_view.add-column-placeholder")}
|
||||||
onBlur={() => finishEdit(true)}
|
save={(columnName) => api.addNewColumn(columnName)}
|
||||||
onKeyDown={(e: KeyboardEvent) => {
|
dismiss={() => setIsCreatingNewColumn(false)}
|
||||||
if (e.key === "Enter") {
|
isNewItem
|
||||||
e.preventDefault();
|
|
||||||
finishEdit(true);
|
|
||||||
} else if (e.key === "Escape") {
|
|
||||||
e.preventDefault();
|
|
||||||
finishEdit(false);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
</>}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function TitleEditor({ currentValue, save, dismiss, multiline, isNewItem }: {
|
export function TitleEditor({ currentValue, placeholder, save, dismiss, multiline, isNewItem }: {
|
||||||
currentValue: string,
|
currentValue?: string;
|
||||||
save: (newValue: string) => void,
|
placeholder?: string;
|
||||||
dismiss: () => void,
|
save: (newValue: string) => void;
|
||||||
multiline?: boolean,
|
dismiss: () => void;
|
||||||
isNewItem?: boolean
|
multiline?: boolean;
|
||||||
|
isNewItem?: boolean;
|
||||||
}) {
|
}) {
|
||||||
const inputRef = useRef<any>(null);
|
const inputRef = useRef<any>(null);
|
||||||
|
|
||||||
@ -276,7 +242,8 @@ export function TitleEditor({ currentValue, save, dismiss, multiline, isNewItem
|
|||||||
return (
|
return (
|
||||||
<Element
|
<Element
|
||||||
inputRef={inputRef}
|
inputRef={inputRef}
|
||||||
currentValue={currentValue}
|
currentValue={currentValue ?? ""}
|
||||||
|
placeholder={placeholder}
|
||||||
rows={multiline ? 4 : undefined}
|
rows={multiline ? 4 : undefined}
|
||||||
onKeyDown={(e) => {
|
onKeyDown={(e) => {
|
||||||
if (e.key === "Enter") {
|
if (e.key === "Enter") {
|
||||||
@ -298,5 +265,5 @@ export function TitleEditor({ currentValue, save, dismiss, multiline, isNewItem
|
|||||||
dismiss();
|
dismiss();
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user