Unify category autocomplete fields
All checks were successful
Deploy Sokko G / deploy (push) Successful in 17s
All checks were successful
Deploy Sokko G / deploy (push) Successful in 17s
This commit is contained in:
parent
e3d823d0b1
commit
055fb78d60
21 changed files with 739 additions and 168 deletions
|
|
@ -1,51 +1,154 @@
|
|||
// Rôle : fournit l'outil liens avec ajout manuel et échange texte.
|
||||
import { useState } from "react";
|
||||
import { CategoryAutocompleteInput } from "../../../components/CategoryAutocompleteInput.jsx";
|
||||
import { Icon } from "../../../components/Icon.jsx";
|
||||
import { useDraftForm } from "../../../hooks/useDraftForm.js";
|
||||
import { moveItem, useGroupedReorder } from "../../../hooks/useGroupedReorder.js";
|
||||
import { applyGroupedReorderOperation, useGroupedReorder } from "../../../hooks/useGroupedReorder.js";
|
||||
import { TextExchangeActions } from "./TextExchangeActions.jsx";
|
||||
|
||||
function getLinkCategory(link) {
|
||||
return String(link?.category || "").trim();
|
||||
}
|
||||
|
||||
function setLinkCategory(link, category) {
|
||||
const cleanCategory = String(category || "").trim();
|
||||
const nextLink = { ...link };
|
||||
if (cleanCategory) nextLink.category = cleanCategory;
|
||||
else delete nextLink.category;
|
||||
return nextLink;
|
||||
}
|
||||
|
||||
function getLinkEntries(data) {
|
||||
const ungrouped = data.links.filter((link) => !getLinkCategory(link)).map((link) => ({ type: "item", item: link }));
|
||||
const categories = data.categoryOrder.map((category) => ({
|
||||
type: "group",
|
||||
group: {
|
||||
category,
|
||||
links: data.links.filter((link) => getLinkCategory(link) === category)
|
||||
}
|
||||
}));
|
||||
return [...ungrouped, ...categories];
|
||||
}
|
||||
|
||||
export function LinksModule({ toolboxId, moduleId, context, editing, textExchangeOpen }) {
|
||||
const data = context.normalizeLinksData(context.getModuleData(toolboxId, moduleId, { links: [] }));
|
||||
const textContent = context.moduleText?.links || {};
|
||||
const linkDraft = useDraftForm({ title: "", url: "" });
|
||||
const linkDraft = useDraftForm({ title: "", url: "", category: "" });
|
||||
const categoryOptions = data.categoryOrder;
|
||||
const [copiedId, setCopiedId] = useState("");
|
||||
const {
|
||||
itemReorder,
|
||||
groupReorder,
|
||||
getItemProps,
|
||||
getGroupProps,
|
||||
getGroupBoundaryProps,
|
||||
isItemDragging,
|
||||
isItemDropTarget,
|
||||
isGroupDragging,
|
||||
isGroupDropTarget,
|
||||
isBoundaryDropTarget,
|
||||
getDropPlacement
|
||||
} = useGroupedReorder({
|
||||
namespace: "links",
|
||||
namespace: `links-${moduleId}`,
|
||||
items: data.links,
|
||||
getItemId: (link) => link.id,
|
||||
getParentId: () => moduleId,
|
||||
onItemMove: (operation) => save(moveItem(data.links, operation.sourceId, operation.targetId, operation.placement)),
|
||||
hierarchy: { enabled: true, stickyParents: true }
|
||||
getItemGroup: getLinkCategory,
|
||||
reorderFeatures: {
|
||||
item: {
|
||||
groupChange: true,
|
||||
boundaryDrop: true,
|
||||
ungroupedDrop: true
|
||||
},
|
||||
group: {
|
||||
reorder: true,
|
||||
boundaryDrop: true,
|
||||
ungroupedDrop: true,
|
||||
itemDrop: true
|
||||
}
|
||||
},
|
||||
onItemMove: applyLinkReorder,
|
||||
onGroupMove: applyLinkReorder,
|
||||
hierarchy: { enabled: false, stickyParents: false }
|
||||
});
|
||||
|
||||
function save(links) {
|
||||
context.setModuleData(toolboxId, moduleId, { links });
|
||||
function save(nextData) {
|
||||
context.setModuleData(toolboxId, moduleId, nextData, "links");
|
||||
}
|
||||
|
||||
function applyLinkReorder(operation) {
|
||||
if (operation.type === "group" && data.categoryOrder.includes(operation.sourceId)) {
|
||||
const categoryOrder = data.categoryOrder.filter((category) => category !== operation.sourceId);
|
||||
const targetIndex = categoryOrder.indexOf(operation.targetId);
|
||||
if (targetIndex >= 0) {
|
||||
categoryOrder.splice(operation.placement === "after" ? targetIndex + 1 : targetIndex, 0, operation.sourceId);
|
||||
save({ ...data, categoryOrder });
|
||||
return;
|
||||
}
|
||||
}
|
||||
const nextData = applyGroupedReorderOperation(data, {
|
||||
operation,
|
||||
itemsKey: "links",
|
||||
groupOrderKey: "categoryOrder",
|
||||
collapsedGroupsKey: "collapsedCategories",
|
||||
getItemGroup: getLinkCategory,
|
||||
setItemGroup: setLinkCategory
|
||||
});
|
||||
save({
|
||||
...nextData,
|
||||
categoryOrder: [
|
||||
...data.categoryOrder,
|
||||
...nextData.categoryOrder.filter((category) => !data.categoryOrder.includes(category))
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
function addLink(event) {
|
||||
linkDraft.handleSubmit(event, (draft, { reset }) => {
|
||||
const cleanUrl = context.normalizeUrl(draft.url);
|
||||
if (!cleanUrl) return;
|
||||
const category = draft.category.trim();
|
||||
|
||||
save([
|
||||
...data.links,
|
||||
{
|
||||
id: context.uid("link"),
|
||||
title: draft.title.trim(),
|
||||
url: cleanUrl
|
||||
if (!cleanUrl) {
|
||||
if (category && !data.categoryOrder.includes(category)) {
|
||||
save({ ...data, categoryOrder: [...data.categoryOrder, category] });
|
||||
reset();
|
||||
}
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
const categoryOrder = category && !data.categoryOrder.includes(category) ? [...data.categoryOrder, category] : data.categoryOrder;
|
||||
save({
|
||||
...data,
|
||||
categoryOrder,
|
||||
links: [
|
||||
...data.links,
|
||||
{
|
||||
id: context.uid("link"),
|
||||
title: draft.title.trim(),
|
||||
url: cleanUrl,
|
||||
...(category ? { category } : {})
|
||||
}
|
||||
]
|
||||
});
|
||||
reset();
|
||||
});
|
||||
}
|
||||
|
||||
function deleteCategory(category) {
|
||||
save({
|
||||
...data,
|
||||
categoryOrder: data.categoryOrder.filter((item) => item !== category),
|
||||
collapsedCategories: data.collapsedCategories.filter((item) => item !== category),
|
||||
links: data.links.map((link) => getLinkCategory(link) === category ? setLinkCategory(link, "") : link)
|
||||
});
|
||||
}
|
||||
|
||||
function toggleCategoryCollapsed(category) {
|
||||
const collapsedCategories = data.collapsedCategories.includes(category)
|
||||
? data.collapsedCategories.filter((item) => item !== category)
|
||||
: [...data.collapsedCategories, category];
|
||||
save({ ...data, collapsedCategories });
|
||||
}
|
||||
|
||||
async function copyUrl(link) {
|
||||
const copied = await context.copyText(link.url);
|
||||
if (!copied) return;
|
||||
|
|
@ -61,6 +164,7 @@ export function LinksModule({ toolboxId, moduleId, context, editing, textExchang
|
|||
<form className="inline-form links-add-form" onSubmit={addLink}>
|
||||
<input {...linkDraft.getFieldProps("title", { className: "nds-input", "data-size": "sm", type: "text", placeholder: textContent.titlePlaceholder || "Nom du lien" })} />
|
||||
<input {...linkDraft.getFieldProps("url", { className: "nds-input", "data-size": "sm", type: "url", placeholder: textContent.urlPlaceholder || "https://..." })} />
|
||||
<CategoryAutocompleteInput {...linkDraft.getFieldProps("category", { className: "nds-input", "data-size": "sm", placeholder: textContent.categoryPlaceholder || "Catégorie" })} options={categoryOptions} />
|
||||
<button className="nds-button" data-size="sm" data-variant="primary">{textContent.addButton || "Ajouter"}</button>
|
||||
</form>
|
||||
</div>
|
||||
|
|
@ -70,44 +174,125 @@ export function LinksModule({ toolboxId, moduleId, context, editing, textExchang
|
|||
<TextExchangeActions type="links" data={data} context={context} textContent={textContent} onImport={(nextData) => context.setModuleData(toolboxId, moduleId, nextData, "links")} />
|
||||
</div>
|
||||
)}
|
||||
<ul className="links-list">
|
||||
{data.links.map((link) => {
|
||||
const className = [
|
||||
"link-item",
|
||||
isItemDragging(link.id) ? "is-dragging" : "",
|
||||
isItemDropTarget(link.id) ? "is-drop-target" : "",
|
||||
getDropPlacement("item", link.id) === "after" ? "drop-after" : ""
|
||||
].filter(Boolean).join(" ");
|
||||
<div className="links-category-list">
|
||||
{getLinkEntries(data).map((entry) => {
|
||||
if (entry.type === "group") {
|
||||
const category = entry.group.category;
|
||||
const isCollapsed = data.collapsedCategories.includes(category);
|
||||
const showBoundaryDropZones = Boolean(itemReorder.draggingId || groupReorder.draggingId);
|
||||
const categoryClassName = [
|
||||
"links-category-section checklist-section is-grouped",
|
||||
isCollapsed ? "is-collapsed" : "",
|
||||
isGroupDragging(category) ? "is-dragging" : "",
|
||||
isGroupDropTarget(category) ? "is-drop-target" : "",
|
||||
getDropPlacement("group", category) === "after" ? "drop-after" : ""
|
||||
].filter(Boolean).join(" ");
|
||||
|
||||
return (
|
||||
<li className={className} key={link.id} {...getItemProps({ itemId: link.id, parentId: moduleId })}>
|
||||
<button
|
||||
className="link-drag-handle nds-icon-button"
|
||||
data-size="sm"
|
||||
data-variant="secondary"
|
||||
type="button"
|
||||
onPointerDown={(event) => itemReorder.startDrag(event, link.id)}
|
||||
aria-label={`${textContent.reorderTitle || "Déplacer"} ${link.title || link.url}`}
|
||||
title={textContent.reorderTitle || "Déplacer"}
|
||||
>
|
||||
<Icon name="drag" />
|
||||
</button>
|
||||
<a href={link.url} target="_blank" rel="noreferrer" title={link.url}>
|
||||
<strong>{link.title || context.hostnameFromUrl(link.url)}</strong>
|
||||
<span>{link.url}</span>
|
||||
</a>
|
||||
<div>
|
||||
<button type="button" className="nds-icon-button" data-size="sm" onClick={() => copyUrl(link)} aria-label={`${textContent.copyTitle || "Copier"} ${link.title || link.url}`} title={copiedId === link.id ? textContent.copiedTitle || "Copié" : textContent.copyTitle || "Copier"}>
|
||||
<Icon name="copy" />
|
||||
</button>
|
||||
<button type="button" className="nds-icon-button" data-size="sm" data-variant="danger" onClick={() => save(data.links.filter((item) => item.id !== link.id))} aria-label={`${textContent.deleteTitle || "Supprimer"} ${link.title || link.url}`} title={textContent.deleteTitle || "Supprimer"}>
|
||||
<Icon name="trash" />
|
||||
</button>
|
||||
</div>
|
||||
</li>
|
||||
);
|
||||
return (
|
||||
<div className="links-category-block" key={`category:${category}`}>
|
||||
{showBoundaryDropZones && (
|
||||
<div
|
||||
className={`links-category-boundary-drop-zone ${isBoundaryDropTarget({ groupId: category, placement: "before" }) ? "is-drop-target" : ""}`}
|
||||
{...getGroupBoundaryProps({ groupId: category, placement: "before" })}
|
||||
/>
|
||||
)}
|
||||
<section className={categoryClassName} {...getGroupProps({ groupId: category })}>
|
||||
<div className="checklist-section-header links-category-header">
|
||||
<div className="links-category-title">
|
||||
<button
|
||||
className="task-planner-category-drag-handle nds-icon-button"
|
||||
data-size="xs"
|
||||
data-variant="secondary"
|
||||
type="button"
|
||||
onPointerDown={(event) => groupReorder.startDrag(event, { groupId: category })}
|
||||
aria-label={`${textContent.reorderCategoryTitle || "Déplacer la catégorie"} ${category}`}
|
||||
title={textContent.reorderCategoryTitle || "Déplacer la catégorie"}
|
||||
>
|
||||
<Icon name="drag" />
|
||||
</button>
|
||||
<h3>{category}</h3>
|
||||
</div>
|
||||
<div className="checklist-section-actions links-category-actions">
|
||||
<span>{entry.group.links.length}</span>
|
||||
<button
|
||||
className="checklist-section-collapse-button nds-icon-button"
|
||||
data-size="xs"
|
||||
type="button"
|
||||
onClick={() => toggleCategoryCollapsed(category)}
|
||||
aria-expanded={!isCollapsed}
|
||||
aria-label={isCollapsed ? textContent.showCategoryTitle || "Afficher la catégorie" : textContent.hideCategoryTitle || "Réduire la catégorie"}
|
||||
title={isCollapsed ? textContent.showCategoryTitle || "Afficher la catégorie" : textContent.hideCategoryTitle || "Réduire la catégorie"}
|
||||
>
|
||||
<Icon name={isCollapsed ? "chevron-down" : "chevron-up"} />
|
||||
</button>
|
||||
<button
|
||||
className="nds-icon-button"
|
||||
data-size="xs"
|
||||
data-variant="danger"
|
||||
type="button"
|
||||
onClick={() => deleteCategory(category)}
|
||||
aria-label={`${textContent.deleteCategoryTitle || "Supprimer la catégorie"} ${category}`}
|
||||
title={textContent.deleteCategoryTitle || "Supprimer la catégorie"}
|
||||
>
|
||||
<Icon name="trash" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{!isCollapsed && (
|
||||
<div className="links-list">
|
||||
{entry.group.links.map(renderLink)}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
{showBoundaryDropZones && (
|
||||
<div
|
||||
className={`links-category-boundary-drop-zone ${isBoundaryDropTarget({ groupId: category, placement: "after" }) ? "is-drop-target" : ""}`}
|
||||
{...getGroupBoundaryProps({ groupId: category, placement: "after" })}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return renderLink(entry.item);
|
||||
})}
|
||||
</ul>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
|
||||
function renderLink(link) {
|
||||
const className = [
|
||||
"link-item",
|
||||
isItemDragging(link.id) ? "is-dragging" : "",
|
||||
isItemDropTarget(link.id) ? "is-drop-target" : "",
|
||||
getDropPlacement("item", link.id) === "after" ? "drop-after" : ""
|
||||
].filter(Boolean).join(" ");
|
||||
|
||||
return (
|
||||
<article className={className} key={link.id} {...getItemProps({ itemId: link.id, groupId: getLinkCategory(link) })}>
|
||||
<button
|
||||
className="link-drag-handle nds-icon-button"
|
||||
data-size="sm"
|
||||
data-variant="secondary"
|
||||
type="button"
|
||||
onPointerDown={(event) => itemReorder.startDrag(event, link.id)}
|
||||
aria-label={`${textContent.reorderTitle || "Déplacer"} ${link.title || link.url}`}
|
||||
title={textContent.reorderTitle || "Déplacer"}
|
||||
>
|
||||
<Icon name="drag" />
|
||||
</button>
|
||||
<a href={link.url} target="_blank" rel="noreferrer" title={link.url}>
|
||||
<strong>{link.title || context.hostnameFromUrl(link.url)}</strong>
|
||||
<span>{link.url}</span>
|
||||
</a>
|
||||
<div>
|
||||
<button type="button" className="nds-icon-button" data-size="sm" onClick={() => copyUrl(link)} aria-label={`${textContent.copyTitle || "Copier"} ${link.title || link.url}`} title={copiedId === link.id ? textContent.copiedTitle || "Copié" : textContent.copyTitle || "Copier"}>
|
||||
<Icon name="copy" />
|
||||
</button>
|
||||
<button type="button" className="nds-icon-button" data-size="sm" data-variant="danger" onClick={() => save({ ...data, links: data.links.filter((item) => item.id !== link.id) })} aria-label={`${textContent.deleteTitle || "Supprimer"} ${link.title || link.url}`} title={textContent.deleteTitle || "Supprimer"}>
|
||||
<Icon name="trash" />
|
||||
</button>
|
||||
</div>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue