Replace plain textarea with shared tiptap editor and sanitized html rendering. Adds interactive checklists on read surfaces and mobile overflow fixes.
29 lines
835 B
TypeScript
29 lines
835 B
TypeScript
const TASK_ITEM_RE = /<li([^>]*data-type="taskItem"[^>]*)>/gi;
|
|
|
|
export function toggleTaskItemInHtml(html: string, taskIndex: number, checked: boolean): string {
|
|
let index = 0;
|
|
return html.replace(TASK_ITEM_RE, (match, attrs: string) => {
|
|
const current = index;
|
|
index += 1;
|
|
if (current !== taskIndex) return match;
|
|
|
|
const checkedValue = checked ? "true" : "false";
|
|
let nextAttrs = attrs;
|
|
|
|
if (/data-checked="(?:true|false)"/i.test(nextAttrs)) {
|
|
nextAttrs = nextAttrs.replace(
|
|
/data-checked="(?:true|false)"/i,
|
|
`data-checked="${checkedValue}"`,
|
|
);
|
|
} else {
|
|
nextAttrs = `${nextAttrs} data-checked="${checkedValue}"`;
|
|
}
|
|
|
|
return `<li${nextAttrs}>`;
|
|
});
|
|
}
|
|
|
|
export function countTaskItems(html: string): number {
|
|
return [...html.matchAll(TASK_ITEM_RE)].length;
|
|
}
|