Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | 76x 76x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 640x 160x 4x 4x 4x 4x 4x 95x 95x 95x 4x 4x 4x 4x 4x 4x 336x 160x 97x 97x 417x 160x 4x 324x 160x 4x 30x 320x 160x 76x 183x 183x 183x 183x 183x 183x 183x 183x 184x 183x 183x 183x 183x 183x 183x 183x 183x 183x 13x 13x 13x 13x 13x 184x 183x 13x 13x 13x 2x 11x 13x 185x 183x 4x 4x 4x 4x 183x 183x 4x 2x 2x 2x 2x 4x 4x 4x 1219x 183x 183x 172x 1x 183x 165x 48x 4x 4x 3x 1x 1x 95x 2x 2x 1338x 8x 7x 199x 527x | import Quill from "quill";
import {
forwardRef,
useCallback,
useEffect,
useImperativeHandle,
useRef,
useState,
} from "react";
import "quill/dist/quill.bubble.css";
import { Bold, Italic, List, ListOrdered, Underline } from "lucide-react";
import { toast } from "sonner";
import {
AccessControlledButton as Button,
AccessControlledInput as Input,
} from "@/ahuora-design-system/ui/accessControlled";
import {
useCoreNoteCreateMutation,
useCoreNoteListQuery,
useCoreNotePartialUpdateMutation,
} from "@/api/apiStore.gen";
import { useFlowsheetAccess } from "@/hooks/flowsheetAccess";
import { useCurrentObject } from "@/hooks/flowsheetObjects";
import Note from "./Note";
type QuillFormatValue = boolean | string | number | null;
type QuillEditorHandle = {
getEditor: () => Quill | null;
};
type QuillEditorProps = {
modules: { toolbar: boolean };
onChange: (value: string) => void;
onChangeSelection: () => void;
placeholder?: string;
readOnly?: boolean;
theme: "bubble";
value: string;
};
const QUILL_MODULES = {
toolbar: false,
};
// Use Quill directly: react-quill still calls ReactDOM.findDOMNode, which is
// unavailable under React 19 and crashes the note editor at runtime.
const QuillEditor = forwardRef<QuillEditorHandle, QuillEditorProps>(
(
{
modules,
onChange,
onChangeSelection,
placeholder,
readOnly = false,
theme,
value,
},
ref,
) => {
const containerRef = useRef<HTMLDivElement>(null);
const editorRef = useRef<Quill | null>(null);
const valueRef = useRef(value);
const onChangeRef = useRef(onChange);
const onChangeSelectionRef = useRef(onChangeSelection);
useEffect(() => {
onChangeRef.current = onChange;
onChangeSelectionRef.current = onChangeSelection;
}, [onChange, onChangeSelection]);
useEffect(() => {
Iif (!containerRef.current || editorRef.current) return;
const editor = new Quill(containerRef.current, {
modules,
placeholder,
readOnly,
theme,
});
editorRef.current = editor;
editor.root.setAttribute("aria-label", "note-text-box");
editor.root.setAttribute("role", "textbox");
editor.root.innerHTML = valueRef.current;
const handleTextChange = () => {
const nextValue = editor.root.innerHTML;
valueRef.current = nextValue;
onChangeRef.current(nextValue);
};
const handleSelectionChange = () => {
onChangeSelectionRef.current();
};
editor.on("text-change", handleTextChange);
editor.on("selection-change", handleSelectionChange);
return () => {
editor.off("text-change", handleTextChange);
editor.off("selection-change", handleSelectionChange);
editorRef.current = null;
};
}, [modules, placeholder, readOnly, theme]);
useEffect(() => {
const editor = editorRef.current;
Iif (!editor || value === valueRef.current) return;
const selection = editor.getSelection();
editor.root.innerHTML = value;
valueRef.current = value;
if (selection) {
editor.setSelection(selection);
}
}, [value]);
useEffect(() => {
editorRef.current?.enable(!readOnly);
}, [readOnly]);
useImperativeHandle(
ref,
() => ({
getEditor: () => editorRef.current,
}),
[],
);
return <div ref={containerRef} />;
},
);
QuillEditor.displayName = "QuillEditor";
// Main Notes component
const Notes = ({ simulationObjectId }: { simulationObjectId?: number }) => {
const [createNote] = useCoreNoteCreateMutation();
const [editNote] = useCoreNotePartialUpdateMutation();
const access = useFlowsheetAccess();
const canMutate = access?.can_edit ?? true;
const currentObj = useCurrentObject();
const simId = simulationObjectId ?? currentObj?.id ?? undefined;
const { data: notes } = useCoreNoteListQuery({
simulationObject: simId,
});
const [title, setTitle] = useState("");
const [content, setContent] = useState("");
const [showNewNote, setShowNewNote] = useState(false);
const [editingNoteId, setEditingNoteId] = useState<number | null>(null);
const initialFormatState = {
bold: false,
italic: false,
underline: false,
list: {
bullet: false,
ordered: false,
},
};
const [activeFormats, setActiveFormats] = useState(initialFormatState);
// Ref for the Quill editor instance
const quillRef = useRef<QuillEditorHandle>(null);
const formatText = useCallback(
(format: string, value?: QuillFormatValue) => {
Iif (!canMutate) return;
const editor = quillRef.current?.getEditor();
Iif (!editor) return;
const range = editor.getSelection();
Iif (!range) return;
if (!editor.hasFocus()) {
editor.focus();
}
const currentFormats = editor.getFormat(range);
// Toggle if no explicit value provided
const nextValue = value !== undefined ? value : !currentFormats[format];
editor.format(format, nextValue);
},
[canMutate],
);
const handleFormat = useCallback(
(format: string, value?: QuillFormatValue) => {
if (!canMutate) {
return;
}
const editor = quillRef.current?.getEditor();
if (!editor) {
return;
}
if (!editor.hasFocus()) {
editor.focus();
}
setActiveFormats((prev) => {
// Case 1: Complex formats with a value (like list: 'bullet')
if (value) {
return {
...prev,
list: {
ordered: value === "ordered" ? !prev.list.ordered : false,
bullet: value === "bullet" ? !prev.list.bullet : false,
},
};
}
// Case 2: Simple boolean formats (bold, italic, etc.)
return {
...prev,
[format]: !prev[format],
};
});
formatText(format, value);
},
[canMutate, formatText],
);
// Update active formats based on current selection in the editor
const handleSelectionChange = useCallback(() => {
const editor = quillRef.current?.getEditor();
Iif (!editor) return;
Iif (!editor.hasFocus()) return;
const rawFormat = editor?.getFormat() ?? {};
const newState = {
bold: !!rawFormat?.bold,
italic: !!rawFormat?.italic,
underline: !!rawFormat?.underline,
list: {
bullet: rawFormat?.list === "bullet",
ordered: rawFormat?.list === "ordered",
},
};
setActiveFormats(newState);
}, []);
// Function to save a new note
const saveNoteHandler = useCallback(async () => {
if (!canMutate) {
return;
}
if (!title.trim()) {
toast.error("Please enter a title for the note");
return;
}
if (!content.trim()) {
toast.error("Please enter some content for the note");
return;
}
if (content.trim().length > 2000) {
toast.error("Note content is too long");
return;
}
if (simId == null) {
toast.error("No simulation object selected");
return;
}
setShowNewNote(false);
let promise;
// PATCH existing note
if (editingNoteId) {
promise = editNote({
id: editingNoteId,
patchedNote: {
title: title.trim(),
content,
simulationObject: simId,
},
});
toast.promise(promise, {
loading: "Updating note...",
success: "Note updated",
error: "Failed to update note",
});
}
// CREATE new note
else {
promise = createNote({
note: {
title: title.trim(),
content,
simulationObject: simId,
},
});
toast.promise(promise, {
loading: "Saving note...",
success: "New note saved",
error: "Failed to save note",
});
}
// Clear the form and editor content
setTitle("");
setContent("");
setEditingNoteId(null);
}, [canMutate, title, content, simId, editingNoteId, editNote, createNote]);
// Function to cancel note creation/editing
const handleCancel = useCallback(() => {
setShowNewNote(false);
setTitle("");
setContent("");
setEditingNoteId(null);
}, []);
// div for the Notes component
return (
<div>
<style>
{`
.ql-editor {
font-family: 'Inter', sans-serif;
color: hsl(var(--foreground));
min-height: 30px;
overflow-wrap: anywhere;
width: 100%;
background-color: hsl(var(--background));
}
.ql-editor.ql-blank::before {
color: hsl(var(--text-faint));
font-family: 'Inter', sans-serif;
font-size: 0.9em;
}
.ql-editor ul, .ql-editor ol {
list-style-position: inside;
color: hsl(var(--foreground));
}
.prose ul, .prose ol {
list-style-position: inside;
color: hsl(var(--foreground));
}
.prose ul {
list-style-type: disc;
}
.prose ol {
list-style-type: decimal;
}
`}
</style>
{showNewNote ? (
<div className="rounded-lg p-4">
<Input
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Title"
className="mb-4"
aria-label="note-title-box"
/>
<div className="flex space-x-2 mb-2">
<Button
aria-label="note-bold"
// variant="ghost"
variant={activeFormats.bold ? "secondary" : "ghost"}
onClick={() => handleFormat("bold")}
>
<Bold className="h-4 w-4" />
</Button>
<Button
aria-label="note-italic"
// variant="ghost"
variant={activeFormats.italic ? "secondary" : "ghost"}
onClick={() => handleFormat("italic")}
>
<Italic className="h-4 w-4" />
</Button>
<Button
aria-label="note-underline"
// variant="ghost"
variant={activeFormats.underline ? "secondary" : "ghost"}
onClick={() => handleFormat("underline")}
>
<Underline className="h-4 w-4" />
</Button>
<Button
aria-label="note-list"
// variant="ghost"
variant={activeFormats.list.bullet ? "secondary" : "ghost"}
onClick={() => handleFormat("list", "bullet")}
>
<List className="h-4 w-4" />
</Button>
<Button
aria-label="note-listordered"
// variant="ghost"
variant={activeFormats.list.ordered ? "secondary" : "ghost"}
onClick={() => handleFormat("list", "ordered")}
>
<ListOrdered className="h-4 w-4" />
</Button>
</div>
<div className="min-h-[30px] border-2 border-gray-600 rounded-md p-0.1 w-full">
<QuillEditor
ref={quillRef}
theme="bubble"
value={content}
onChange={(e) => {
setContent(e);
}}
readOnly={!canMutate}
onChangeSelection={handleSelectionChange}
modules={QUILL_MODULES}
placeholder="Type a note..."
/>
</div>
<div className="flex justify-end mt-4 space-x-2">
<Button variant="outline" onClick={handleCancel}>
Cancel
</Button>
<Button onClick={saveNoteHandler}>Save</Button>
</div>
</div>
) : (
<Button
onClick={() => {
setShowNewNote(true);
setActiveFormats(initialFormatState);
}}
className="w-full"
aria-label="New Note"
>
New note
</Button>
)}
{notes?.map((note) => (
<Note
key={note.id}
data={note}
showEditor={setShowNewNote}
setNoteTitle={setTitle}
setNoteContent={setContent}
setEditingNoteId={setEditingNoteId}
canMutate={canMutate}
/>
))}
</div>
);
};
export default Notes;
|