a tool for shared writing and social publishing
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

group selection manager shortcuts

+232 -222
+232 -222
components/SelectionManager.tsx
··· 225 225 handler: () => undoManager.withUndoGroup(() => shortcut.handler()), 226 226 })), 227 227 ); 228 - let listener = async (e: KeyboardEvent) => { 229 - if (e.key === "Backspace" || e.key === "Delete") { 230 - if (!entity_set.permissions.write) return; 231 - if (moreThanOneSelected) { 232 - e.preventDefault(); 233 - let [sortedBlocks, siblings] = await getSortedSelection(); 234 - let selectedBlocks = useUIState.getState().selectedBlocks; 235 - let firstBlock = sortedBlocks[0]; 228 + let listener = async (e: KeyboardEvent) => 229 + undoManager.withUndoGroup(async () => { 230 + if (e.key === "Backspace" || e.key === "Delete") { 231 + if (!entity_set.permissions.write) return; 232 + if (moreThanOneSelected) { 233 + e.preventDefault(); 234 + let [sortedBlocks, siblings] = await getSortedSelection(); 235 + let selectedBlocks = useUIState.getState().selectedBlocks; 236 + let firstBlock = sortedBlocks[0]; 236 237 237 - await rep?.mutate.removeBlock( 238 - selectedBlocks.map((block) => ({ blockEntity: block.value })), 239 - ); 240 - useUIState.getState().closePage(selectedBlocks.map((b) => b.value)); 238 + await rep?.mutate.removeBlock( 239 + selectedBlocks.map((block) => ({ blockEntity: block.value })), 240 + ); 241 + useUIState.getState().closePage(selectedBlocks.map((b) => b.value)); 241 242 242 - let nextBlock = 243 - siblings?.[ 244 - siblings.findIndex((s) => s.value === firstBlock.value) - 1 245 - ]; 246 - if (nextBlock) { 247 - useUIState.getState().setSelectedBlock({ 248 - value: nextBlock.value, 249 - parent: nextBlock.parent, 250 - }); 243 + let nextBlock = 244 + siblings?.[ 245 + siblings.findIndex((s) => s.value === firstBlock.value) - 1 246 + ]; 247 + if (nextBlock) { 248 + useUIState.getState().setSelectedBlock({ 249 + value: nextBlock.value, 250 + parent: nextBlock.parent, 251 + }); 252 + let type = await rep?.query((tx) => 253 + scanIndex(tx).eav(nextBlock.value, "block/type"), 254 + ); 255 + if (!type?.[0]) return; 256 + if ( 257 + type[0]?.data.value === "text" || 258 + type[0]?.data.value === "heading" 259 + ) 260 + focusBlock( 261 + { 262 + value: nextBlock.value, 263 + type: "text", 264 + parent: nextBlock.parent, 265 + }, 266 + { type: "end" }, 267 + ); 268 + } 269 + } 270 + } 271 + if (e.key === "ArrowUp") { 272 + let [sortedBlocks, siblings] = await getSortedSelection(); 273 + let focusedBlock = useUIState.getState().focusedEntity; 274 + if (!e.shiftKey && !e.ctrlKey) { 275 + if (e.defaultPrevented) return; 276 + if (sortedBlocks.length === 1) return; 277 + let firstBlock = sortedBlocks[0]; 278 + if (!firstBlock) return; 251 279 let type = await rep?.query((tx) => 252 - scanIndex(tx).eav(nextBlock.value, "block/type"), 280 + scanIndex(tx).eav(firstBlock.value, "block/type"), 253 281 ); 254 282 if (!type?.[0]) return; 283 + useUIState.getState().setSelectedBlock(firstBlock); 284 + focusBlock( 285 + { ...firstBlock, type: type[0].data.value }, 286 + { type: "start" }, 287 + ); 288 + } else { 289 + if (e.defaultPrevented) return; 255 290 if ( 256 - type[0]?.data.value === "text" || 257 - type[0]?.data.value === "heading" 291 + sortedBlocks.length <= 1 || 292 + !focusedBlock || 293 + focusedBlock.entityType === "page" 258 294 ) 259 - focusBlock( 260 - { 261 - value: nextBlock.value, 262 - type: "text", 263 - parent: nextBlock.parent, 264 - }, 265 - { type: "end" }, 295 + return; 296 + let b = focusedBlock; 297 + let focusedBlockIndex = sortedBlocks.findIndex( 298 + (s) => s.value == b.entityID, 299 + ); 300 + if (focusedBlockIndex === 0) { 301 + let index = siblings.findIndex((s) => s.value === b.entityID); 302 + let nextSelectedBlock = siblings[index - 1]; 303 + if (!nextSelectedBlock) return; 304 + 305 + scrollIntoViewIfNeeded( 306 + document.getElementById( 307 + elementId.block(nextSelectedBlock.value).container, 308 + ), 309 + false, 310 + ); 311 + useUIState.getState().addBlockToSelection({ 312 + ...nextSelectedBlock, 313 + }); 314 + useUIState.getState().setFocusedBlock({ 315 + entityType: "block", 316 + parent: nextSelectedBlock.parent, 317 + entityID: nextSelectedBlock.value, 318 + }); 319 + } else { 320 + let nextBlock = sortedBlocks[sortedBlocks.length - 2]; 321 + useUIState.getState().setFocusedBlock({ 322 + entityType: "block", 323 + parent: b.parent, 324 + entityID: nextBlock.value, 325 + }); 326 + scrollIntoViewIfNeeded( 327 + document.getElementById( 328 + elementId.block(nextBlock.value).container, 329 + ), 330 + false, 266 331 ); 332 + if (sortedBlocks.length === 2) { 333 + useEditorStates 334 + .getState() 335 + .editorStates[nextBlock.value]?.view?.focus(); 336 + } 337 + useUIState 338 + .getState() 339 + .removeBlockFromSelection(sortedBlocks[focusedBlockIndex]); 340 + } 267 341 } 268 342 } 269 - } 270 - if (e.key === "ArrowUp") { 271 - let [sortedBlocks, siblings] = await getSortedSelection(); 272 - let focusedBlock = useUIState.getState().focusedEntity; 273 - if (!e.shiftKey && !e.ctrlKey) { 274 - if (e.defaultPrevented) return; 275 - if (sortedBlocks.length === 1) return; 276 - let firstBlock = sortedBlocks[0]; 343 + if (e.key === "ArrowLeft") { 344 + let [sortedSelection, siblings] = await getSortedSelection(); 345 + if (sortedSelection.length === 1) return; 346 + let firstBlock = sortedSelection[0]; 277 347 if (!firstBlock) return; 278 348 let type = await rep?.query((tx) => 279 349 scanIndex(tx).eav(firstBlock.value, "block/type"), ··· 284 354 { ...firstBlock, type: type[0].data.value }, 285 355 { type: "start" }, 286 356 ); 287 - } else { 288 - if (e.defaultPrevented) return; 289 - if ( 290 - sortedBlocks.length <= 1 || 291 - !focusedBlock || 292 - focusedBlock.entityType === "page" 293 - ) 294 - return; 295 - let b = focusedBlock; 296 - let focusedBlockIndex = sortedBlocks.findIndex( 297 - (s) => s.value == b.entityID, 298 - ); 299 - if (focusedBlockIndex === 0) { 300 - let index = siblings.findIndex((s) => s.value === b.entityID); 301 - let nextSelectedBlock = siblings[index - 1]; 302 - if (!nextSelectedBlock) return; 303 - 304 - scrollIntoViewIfNeeded( 305 - document.getElementById( 306 - elementId.block(nextSelectedBlock.value).container, 307 - ), 308 - false, 309 - ); 310 - useUIState.getState().addBlockToSelection({ 311 - ...nextSelectedBlock, 312 - }); 313 - useUIState.getState().setFocusedBlock({ 314 - entityType: "block", 315 - parent: nextSelectedBlock.parent, 316 - entityID: nextSelectedBlock.value, 317 - }); 318 - } else { 319 - let nextBlock = sortedBlocks[sortedBlocks.length - 2]; 320 - useUIState.getState().setFocusedBlock({ 321 - entityType: "block", 322 - parent: b.parent, 323 - entityID: nextBlock.value, 324 - }); 325 - scrollIntoViewIfNeeded( 326 - document.getElementById( 327 - elementId.block(nextBlock.value).container, 328 - ), 329 - false, 330 - ); 331 - if (sortedBlocks.length === 2) { 332 - useEditorStates 333 - .getState() 334 - .editorStates[nextBlock.value]?.view?.focus(); 335 - } 336 - useUIState 337 - .getState() 338 - .removeBlockFromSelection(sortedBlocks[focusedBlockIndex]); 339 - } 340 357 } 341 - } 342 - if (e.key === "ArrowLeft") { 343 - let [sortedSelection, siblings] = await getSortedSelection(); 344 - if (sortedSelection.length === 1) return; 345 - let firstBlock = sortedSelection[0]; 346 - if (!firstBlock) return; 347 - let type = await rep?.query((tx) => 348 - scanIndex(tx).eav(firstBlock.value, "block/type"), 349 - ); 350 - if (!type?.[0]) return; 351 - useUIState.getState().setSelectedBlock(firstBlock); 352 - focusBlock( 353 - { ...firstBlock, type: type[0].data.value }, 354 - { type: "start" }, 355 - ); 356 - } 357 - if (e.key === "ArrowRight") { 358 - let [sortedSelection, siblings] = await getSortedSelection(); 359 - if (sortedSelection.length === 1) return; 360 - let lastBlock = sortedSelection[sortedSelection.length - 1]; 361 - if (!lastBlock) return; 362 - let type = await rep?.query((tx) => 363 - scanIndex(tx).eav(lastBlock.value, "block/type"), 364 - ); 365 - if (!type?.[0]) return; 366 - useUIState.getState().setSelectedBlock(lastBlock); 367 - focusBlock({ ...lastBlock, type: type[0].data.value }, { type: "end" }); 368 - } 369 - if (e.key === "Tab") { 370 - let [sortedSelection, siblings] = await getSortedSelection(); 371 - if (sortedSelection.length <= 1) return; 372 - e.preventDefault(); 373 - if (e.shiftKey) { 374 - for (let i = siblings.length - 1; i >= 0; i--) { 375 - let block = siblings[i]; 376 - if (!sortedSelection.find((s) => s.value === block.value)) continue; 377 - if (sortedSelection.find((s) => s.value === block.listData?.parent)) 378 - continue; 379 - let parentoffset = 1; 380 - let previousBlock = siblings[i - parentoffset]; 381 - while ( 382 - previousBlock && 383 - sortedSelection.find((s) => previousBlock.value === s.value) 384 - ) { 385 - parentoffset += 1; 386 - previousBlock = siblings[i - parentoffset]; 387 - } 388 - if (!block.listData || !previousBlock.listData) continue; 389 - outdent(block, previousBlock, rep); 390 - } 391 - } else { 392 - for (let i = 0; i < siblings.length; i++) { 393 - let block = siblings[i]; 394 - if (!sortedSelection.find((s) => s.value === block.value)) continue; 395 - if (sortedSelection.find((s) => s.value === block.listData?.parent)) 396 - continue; 397 - let parentoffset = 1; 398 - let previousBlock = siblings[i - parentoffset]; 399 - while ( 400 - previousBlock && 401 - sortedSelection.find((s) => previousBlock.value === s.value) 402 - ) { 403 - parentoffset += 1; 404 - previousBlock = siblings[i - parentoffset]; 405 - } 406 - if (!block.listData || !previousBlock.listData) continue; 407 - indent(block, previousBlock, rep); 408 - } 409 - } 410 - } 411 - if (e.key === "ArrowDown") { 412 - let [sortedSelection, siblings] = await getSortedSelection(); 413 - let focusedBlock = useUIState.getState().focusedEntity; 414 - if (!e.shiftKey) { 358 + if (e.key === "ArrowRight") { 359 + let [sortedSelection, siblings] = await getSortedSelection(); 415 360 if (sortedSelection.length === 1) return; 416 361 let lastBlock = sortedSelection[sortedSelection.length - 1]; 417 362 if (!lastBlock) return; ··· 425 370 { type: "end" }, 426 371 ); 427 372 } 428 - if (e.shiftKey) { 429 - if (e.defaultPrevented) return; 430 - if ( 431 - sortedSelection.length <= 1 || 432 - !focusedBlock || 433 - focusedBlock.entityType === "page" 434 - ) 435 - return; 436 - let b = focusedBlock; 437 - let focusedBlockIndex = sortedSelection.findIndex( 438 - (s) => s.value == b.entityID, 439 - ); 440 - if (focusedBlockIndex === sortedSelection.length - 1) { 441 - let index = siblings.findIndex((s) => s.value === b.entityID); 442 - let nextSelectedBlock = siblings[index + 1]; 443 - if (!nextSelectedBlock) return; 444 - useUIState.getState().addBlockToSelection({ 445 - ...nextSelectedBlock, 446 - }); 447 - 448 - scrollIntoViewIfNeeded( 449 - document.getElementById( 450 - elementId.block(nextSelectedBlock.value).container, 451 - ), 452 - false, 373 + if (e.key === "Tab") { 374 + let [sortedSelection, siblings] = await getSortedSelection(); 375 + if (sortedSelection.length <= 1) return; 376 + e.preventDefault(); 377 + if (e.shiftKey) { 378 + for (let i = siblings.length - 1; i >= 0; i--) { 379 + let block = siblings[i]; 380 + if (!sortedSelection.find((s) => s.value === block.value)) 381 + continue; 382 + if ( 383 + sortedSelection.find((s) => s.value === block.listData?.parent) 384 + ) 385 + continue; 386 + let parentoffset = 1; 387 + let previousBlock = siblings[i - parentoffset]; 388 + while ( 389 + previousBlock && 390 + sortedSelection.find((s) => previousBlock.value === s.value) 391 + ) { 392 + parentoffset += 1; 393 + previousBlock = siblings[i - parentoffset]; 394 + } 395 + if (!block.listData || !previousBlock.listData) continue; 396 + outdent(block, previousBlock, rep); 397 + } 398 + } else { 399 + for (let i = 0; i < siblings.length; i++) { 400 + let block = siblings[i]; 401 + if (!sortedSelection.find((s) => s.value === block.value)) 402 + continue; 403 + if ( 404 + sortedSelection.find((s) => s.value === block.listData?.parent) 405 + ) 406 + continue; 407 + let parentoffset = 1; 408 + let previousBlock = siblings[i - parentoffset]; 409 + while ( 410 + previousBlock && 411 + sortedSelection.find((s) => previousBlock.value === s.value) 412 + ) { 413 + parentoffset += 1; 414 + previousBlock = siblings[i - parentoffset]; 415 + } 416 + if (!block.listData || !previousBlock.listData) continue; 417 + indent(block, previousBlock, rep); 418 + } 419 + } 420 + } 421 + if (e.key === "ArrowDown") { 422 + let [sortedSelection, siblings] = await getSortedSelection(); 423 + let focusedBlock = useUIState.getState().focusedEntity; 424 + if (!e.shiftKey) { 425 + if (sortedSelection.length === 1) return; 426 + let lastBlock = sortedSelection[sortedSelection.length - 1]; 427 + if (!lastBlock) return; 428 + let type = await rep?.query((tx) => 429 + scanIndex(tx).eav(lastBlock.value, "block/type"), 430 + ); 431 + if (!type?.[0]) return; 432 + useUIState.getState().setSelectedBlock(lastBlock); 433 + focusBlock( 434 + { ...lastBlock, type: type[0].data.value }, 435 + { type: "end" }, 453 436 ); 454 - useUIState.getState().setFocusedBlock({ 455 - entityType: "block", 456 - parent: nextSelectedBlock.parent, 457 - entityID: nextSelectedBlock.value, 458 - }); 459 - } else { 460 - let nextBlock = sortedSelection[1]; 461 - useUIState 462 - .getState() 463 - .removeBlockFromSelection({ value: b.entityID }); 464 - scrollIntoViewIfNeeded( 465 - document.getElementById( 466 - elementId.block(nextBlock.value).container, 467 - ), 468 - false, 437 + } 438 + if (e.shiftKey) { 439 + if (e.defaultPrevented) return; 440 + if ( 441 + sortedSelection.length <= 1 || 442 + !focusedBlock || 443 + focusedBlock.entityType === "page" 444 + ) 445 + return; 446 + let b = focusedBlock; 447 + let focusedBlockIndex = sortedSelection.findIndex( 448 + (s) => s.value == b.entityID, 469 449 ); 470 - useUIState.getState().setFocusedBlock({ 471 - entityType: "block", 472 - parent: b.parent, 473 - entityID: nextBlock.value, 474 - }); 475 - if (sortedSelection.length === 2) { 476 - useEditorStates 450 + if (focusedBlockIndex === sortedSelection.length - 1) { 451 + let index = siblings.findIndex((s) => s.value === b.entityID); 452 + let nextSelectedBlock = siblings[index + 1]; 453 + if (!nextSelectedBlock) return; 454 + useUIState.getState().addBlockToSelection({ 455 + ...nextSelectedBlock, 456 + }); 457 + 458 + scrollIntoViewIfNeeded( 459 + document.getElementById( 460 + elementId.block(nextSelectedBlock.value).container, 461 + ), 462 + false, 463 + ); 464 + useUIState.getState().setFocusedBlock({ 465 + entityType: "block", 466 + parent: nextSelectedBlock.parent, 467 + entityID: nextSelectedBlock.value, 468 + }); 469 + } else { 470 + let nextBlock = sortedSelection[1]; 471 + useUIState 477 472 .getState() 478 - .editorStates[nextBlock.value]?.view?.focus(); 473 + .removeBlockFromSelection({ value: b.entityID }); 474 + scrollIntoViewIfNeeded( 475 + document.getElementById( 476 + elementId.block(nextBlock.value).container, 477 + ), 478 + false, 479 + ); 480 + useUIState.getState().setFocusedBlock({ 481 + entityType: "block", 482 + parent: b.parent, 483 + entityID: nextBlock.value, 484 + }); 485 + if (sortedSelection.length === 2) { 486 + useEditorStates 487 + .getState() 488 + .editorStates[nextBlock.value]?.view?.focus(); 489 + } 479 490 } 480 491 } 481 492 } 482 - } 483 - if (e.key === "c" && (e.metaKey || e.ctrlKey)) { 484 - if (!rep) return; 485 - let [, , selectionWithFoldedChildren] = await getSortedSelection(); 486 - if (!selectionWithFoldedChildren) return; 487 - await copySelection(rep, selectionWithFoldedChildren); 488 - } 489 - }; 493 + if (e.key === "c" && (e.metaKey || e.ctrlKey)) { 494 + if (!rep) return; 495 + let [, , selectionWithFoldedChildren] = await getSortedSelection(); 496 + if (!selectionWithFoldedChildren) return; 497 + await copySelection(rep, selectionWithFoldedChildren); 498 + } 499 + }); 490 500 window.addEventListener("keydown", listener); 491 501 return () => { 492 502 removeListener();