@dou-ui/editor 0.1.0-alpha.11 → 0.1.0-alpha.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/dist/collaboration/cursors.js +42 -10
  2. package/dist/collaboration/cursors.js.map +1 -1
  3. package/dist/collaboration/index.js +30 -3
  4. package/dist/collaboration/index.js.map +1 -1
  5. package/dist/components/block-handle.js.map +1 -1
  6. package/dist/components/block-handle.vue_vue_type_script_setup_true_lang.js +73 -15
  7. package/dist/components/block-handle.vue_vue_type_script_setup_true_lang.js.map +1 -1
  8. package/dist/components/slash-choices.js +6 -0
  9. package/dist/components/slash-choices.js.map +1 -0
  10. package/dist/components/slash-choices.vue_vue_type_script_setup_true_lang.js +56 -0
  11. package/dist/components/slash-choices.vue_vue_type_script_setup_true_lang.js.map +1 -0
  12. package/dist/components/slash-menu.js.map +1 -1
  13. package/dist/components/slash-menu.vue_vue_type_script_setup_true_lang.js +185 -20
  14. package/dist/components/slash-menu.vue_vue_type_script_setup_true_lang.js.map +1 -1
  15. package/dist/core/block-selection.js +32 -4
  16. package/dist/core/block-selection.js.map +1 -1
  17. package/dist/core/blocks.js +21 -3
  18. package/dist/core/blocks.js.map +1 -1
  19. package/dist/core/commands.js +125 -13
  20. package/dist/core/commands.js.map +1 -1
  21. package/dist/core/extension.js +6 -2
  22. package/dist/core/extension.js.map +1 -1
  23. package/dist/core/model.js +8 -2
  24. package/dist/core/model.js.map +1 -1
  25. package/dist/core/schema.js.map +1 -1
  26. package/dist/core/transaction.js +12 -3
  27. package/dist/core/transaction.js.map +1 -1
  28. package/dist/document.js +40 -4
  29. package/dist/document.js.map +1 -1
  30. package/dist/dom.js +96 -13
  31. package/dist/dom.js.map +1 -1
  32. package/dist/editor.vue_vue_type_script_setup_true_lang.js +3 -3
  33. package/dist/editor.vue_vue_type_script_setup_true_lang.js.map +1 -1
  34. package/dist/editor2.js.map +1 -1
  35. package/dist/extensions/columns/column-picker.js +6 -0
  36. package/dist/extensions/columns/column-picker.js.map +1 -0
  37. package/dist/extensions/columns/column-picker.vue_vue_type_script_setup_true_lang.js +57 -0
  38. package/dist/extensions/columns/column-picker.vue_vue_type_script_setup_true_lang.js.map +1 -0
  39. package/dist/extensions/columns/columns-controls.js +6 -0
  40. package/dist/extensions/columns/columns-controls.js.map +1 -0
  41. package/dist/extensions/columns/columns-controls.vue_vue_type_script_setup_true_lang.js +341 -0
  42. package/dist/extensions/columns/columns-controls.vue_vue_type_script_setup_true_lang.js.map +1 -0
  43. package/dist/extensions/columns/html.js +34 -0
  44. package/dist/extensions/columns/html.js.map +1 -0
  45. package/dist/extensions/columns/index.js +57 -0
  46. package/dist/extensions/columns/index.js.map +1 -0
  47. package/dist/extensions/columns/keys.js +53 -0
  48. package/dist/extensions/columns/keys.js.map +1 -0
  49. package/dist/extensions/columns/plugin.js +213 -0
  50. package/dist/extensions/columns/plugin.js.map +1 -0
  51. package/dist/extensions/columns/schema.js +58 -0
  52. package/dist/extensions/columns/schema.js.map +1 -0
  53. package/dist/extensions/columns/select-columns.js +6 -0
  54. package/dist/extensions/columns/select-columns.js.map +1 -0
  55. package/dist/extensions/columns/select-columns.vue_vue_type_script_setup_true_lang.js +71 -0
  56. package/dist/extensions/columns/select-columns.vue_vue_type_script_setup_true_lang.js.map +1 -0
  57. package/dist/extensions/editor-kit.js +3 -1
  58. package/dist/extensions/editor-kit.js.map +1 -1
  59. package/dist/extensions/table/plugin.js +33 -1
  60. package/dist/extensions/table/plugin.js.map +1 -1
  61. package/dist/extensions/table/table-controls.js.map +1 -1
  62. package/dist/extensions/table/table-controls.vue_vue_type_script_setup_true_lang.js +1 -1
  63. package/dist/extensions/table/table-controls.vue_vue_type_script_setup_true_lang.js.map +1 -1
  64. package/dist/extensions/table/view.js +25 -1
  65. package/dist/extensions/table/view.js.map +1 -1
  66. package/dist/index.js +3 -2
  67. package/dist/index.js.map +1 -1
  68. package/dist/locale.js +30 -0
  69. package/dist/locale.js.map +1 -1
  70. package/dist/style.css +1 -1
  71. package/dist/types/collaboration/cursors.d.ts +29 -1
  72. package/dist/types/collaboration/index.d.ts +17 -2
  73. package/dist/types/components/slash-choices.vue.d.ts +18 -0
  74. package/dist/types/core/blocks.d.ts +10 -0
  75. package/dist/types/core/commands.d.ts +17 -2
  76. package/dist/types/core/extension.d.ts +53 -2
  77. package/dist/types/core/model.d.ts +6 -1
  78. package/dist/types/core/schema.d.ts +1 -1
  79. package/dist/types/document.d.ts +4 -2
  80. package/dist/types/dom.d.ts +34 -4
  81. package/dist/types/editor.vue.d.ts +10 -0
  82. package/dist/types/extensions/columns/column-picker.vue.d.ts +10 -0
  83. package/dist/types/extensions/columns/columns-controls.vue.d.ts +3 -0
  84. package/dist/types/extensions/columns/html.d.ts +10 -0
  85. package/dist/types/extensions/columns/index.d.ts +9 -0
  86. package/dist/types/extensions/columns/keys.d.ts +6 -0
  87. package/dist/types/extensions/columns/plugin.d.ts +28 -0
  88. package/dist/types/extensions/columns/schema.d.ts +17 -0
  89. package/dist/types/extensions/columns/select-columns.vue.d.ts +13 -0
  90. package/dist/types/extensions/editor-kit.d.ts +4 -1
  91. package/dist/types/extensions/table/view.d.ts +8 -1
  92. package/dist/types/index.d.ts +12 -2
  93. package/dist/types/locale.d.ts +17 -0
  94. package/package.json +5 -5
@@ -1 +1 @@
1
- {"version":3,"file":"slash-menu.vue_vue_type_script_setup_true_lang.js","names":["_openBlock","_createBlock","_Transition","_withCtx","_createElementBlock","_unref","_normalizeStyle","_withModifiers","_Fragment","_renderList","_normalizeClass","_createElementVNode","_resolveDynamicComponent","_toDisplayString","_createCommentVNode"],"sources":["../../src/components/slash-menu.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport { isComposing, useFloating, useLayer, useZIndex, type VirtualElement } from 'dou-ui'\nimport { computed, inject, nextTick, shallowRef, useId, useTemplateRef, watch } from 'vue'\nimport { filterOptions } from '../catalog'\nimport { labelOf, type BlockOption } from '../core/extension'\nimport { insertBlockAfter, setBlockType } from '../core/commands'\nimport { slashKey, type SlashState } from '../core/slash'\nimport { editorKey } from '../editor'\n\nconst props = defineProps<{ slash: SlashState | null }>()\n\nconst { view, messages, kit } = inject(editorKey)!\nconst id = `dou-editor-slash-${useId()}`\nconst panel = useTemplateRef<HTMLElement>('panel')\nconst active = shallowRef(0)\n\nconst options = computed(() =>\n props.slash ? filterOptions(kit.blockOptions, props.slash.query, messages.value) : [],\n)\nconst open = computed(() => !!props.slash)\n\n// the menu hangs below the `/` that opened it\nconst anchor: VirtualElement = {\n getBoundingClientRect() {\n const current = view.value\n const from = props.slash?.from\n if (!current || from === undefined) return new DOMRect()\n const { left, top, bottom } = current.coordsAtPos(from)\n return new DOMRect(left, top, 0, bottom - top)\n },\n}\nconst { style, placement, update } = useFloating(() => (open.value ? anchor : null), panel, {\n open,\n placement: 'bottom-start',\n offset: 6,\n})\n// stacked like DouUI's own popups, above whatever opened before\nconst { next } = useZIndex()\nconst zIndex = shallowRef(0)\nwatch(open, (value) => value && (zIndex.value = next()), { immediate: true })\n// a press outside the editor and the menu closes it; inside, the cursor move does\nuseLayer({\n open,\n elements: () => [panel.value, view.value?.dom],\n onOutside: () => close(),\n onEscape: () => close(),\n})\n\nwatch(\n () => props.slash?.query,\n () => {\n active.value = 0\n void nextTick(update)\n },\n)\n\n// the editor keeps focus; the active option is announced through it\nwatch(\n [open, active, options],\n () => {\n const dom = view.value?.dom\n if (!dom) return\n const option = open.value && options.value.length ? `${id}-${active.value}` : null\n if (open.value) {\n dom.setAttribute('aria-controls', id)\n dom.setAttribute('aria-expanded', 'true')\n } else {\n dom.removeAttribute('aria-controls')\n dom.removeAttribute('aria-expanded')\n }\n if (option) dom.setAttribute('aria-activedescendant', option)\n else dom.removeAttribute('aria-activedescendant')\n if (option) document.getElementById(option)?.scrollIntoView({ block: 'nearest' })\n },\n { flush: 'post' },\n)\n\nfunction choose(option: BlockOption) {\n const current = view.value\n const slash = props.slash\n if (!current || !slash) return\n // remove the `/query`, then turn the block, or add one below when it has text\n const removed = current.state.tr.delete(slash.from, current.state.selection.from)\n current.dispatch(removed.setMeta(slashKey, null))\n const { $from } = current.state.selection\n if ($from.parent.content.size > 0) insertBlockAfter($from.pos)(current.state, current.dispatch)\n if (option.type !== 'paragraph' || $from.parent.content.size === 0)\n setBlockType(option.type, option.props)(current.state, current.dispatch)\n current.focus()\n}\n\nfunction close() {\n const current = view.value\n if (current) current.dispatch(current.state.tr.setMeta(slashKey, null))\n}\n\n/** Keys the editor passes on while the menu is open. */\nfunction onKey(event: KeyboardEvent): boolean {\n if (isComposing(event)) return false\n const count = options.value.length\n switch (event.key) {\n case 'ArrowDown':\n case 'ArrowUp':\n if (!count) return false\n active.value = (active.value + (event.key === 'ArrowDown' ? 1 : -1) + count) % count\n return true\n case 'Enter':\n case 'Tab': {\n const option = options.value[active.value]\n if (!option) {\n close()\n return false\n }\n choose(option)\n return true\n }\n default:\n return false\n }\n}\n\ndefineExpose({ onKey })\n</script>\n\n<template>\n <Transition name=\"dou-floating\">\n <div\n v-if=\"open\"\n :id=\"id\"\n ref=\"panel\"\n role=\"listbox\"\n :aria-label=\"messages.blockTypes\"\n class=\"dou-dropdown dou-floating dou-editor-slash\"\n :style=\"[style, { zIndex }]\"\n :data-placement=\"placement\"\n @mousedown.prevent\n >\n <div\n v-for=\"(option, index) in options\"\n :id=\"`${id}-${index}`\"\n :key=\"option.key\"\n role=\"option\"\n :aria-selected=\"index === active\"\n :class=\"['dou-dropdown__item', 'dou-editor-slash__item', { 'is-active': index === active }]\"\n @pointermove=\"active = index\"\n @click=\"choose(option)\"\n >\n <span class=\"dou-editor-slash__icon\"><component :is=\"option.icon\" /></span>\n <span class=\"dou-editor-slash__text\">\n <span class=\"dou-editor-slash__label\">{{ labelOf(option.label, messages) }}</span>\n <span v-if=\"option.hint\" class=\"dou-editor-slash__hint\">{{\n labelOf(option.hint, messages)\n }}</span>\n </span>\n <kbd v-if=\"option.shortcut\" class=\"dou-editor-slash__shortcut\">{{ option.shortcut }}</kbd>\n </div>\n <div v-if=\"!options.length\" class=\"dou-editor-slash__empty\">{{ messages.noResults }}</div>\n </div>\n </Transition>\n</template>\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EASA,MAAM,QAAQ;EAEd,MAAM,EAAE,MAAM,UAAU,QAAQ,OAAO,SAAS;EAChD,MAAM,KAAK,oBAAoB,MAAM;EACrC,MAAM,QAAQ,eAA4B,OAAO;EACjD,MAAM,SAAS,WAAW,CAAC;EAE3B,MAAM,UAAU,eACd,MAAM,QAAQ,cAAc,IAAI,cAAc,MAAM,MAAM,OAAO,SAAS,KAAK,IAAI,CAAC,CACtF;EACA,MAAM,OAAO,eAAe,CAAC,CAAC,MAAM,KAAK;EAGzC,MAAM,SAAyB,EAC7B,wBAAwB;GACtB,MAAM,UAAU,KAAK;GACrB,MAAM,OAAO,MAAM,OAAO;GAC1B,IAAI,CAAC,WAAW,SAAS,KAAA,GAAW,OAAO,IAAI,QAAQ;GACvD,MAAM,EAAE,MAAM,KAAK,WAAW,QAAQ,YAAY,IAAI;GACtD,OAAO,IAAI,QAAQ,MAAM,KAAK,GAAG,SAAS,GAAG;EAC/C,EACF;EACA,MAAM,EAAE,OAAO,WAAW,WAAW,kBAAmB,KAAK,QAAQ,SAAS,MAAO,OAAO;GAC1F;GACA,WAAW;GACX,QAAQ;EACV,CAAC;EAED,MAAM,EAAE,SAAS,UAAU;EAC3B,MAAM,SAAS,WAAW,CAAC;EAC3B,MAAM,OAAO,UAAU,UAAU,OAAO,QAAQ,KAAK,IAAI,EAAE,WAAW,KAAK,CAAC;EAE5E,SAAS;GACP;GACA,gBAAgB,CAAC,MAAM,OAAO,KAAK,OAAO,GAAG;GAC7C,iBAAiB,MAAM;GACvB,gBAAgB,MAAM;EACxB,CAAC;EAED,YACQ,MAAM,OAAO,aACb;GACJ,OAAO,QAAQ;GACf,SAAc,MAAM;EACtB,CACF;EAGA,MACE;GAAC;GAAM;GAAQ;EAAO,SAChB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MAChED,mBAKO,QALP,YAKO,CAJLA,mBAAkF,QAAlF,YAAkFE,gBAAzCR,MAAA,OAAA,CAAO,CAAC,OAAO,OAAOA,MAAA,QAAA,CAAQ,CAAA,GAAA,CAAA,GAC3D,OAAO,QAAnBL,UAAA,GAAAI,mBAES,QAFT,YAESS,gBADPR,MAAA,OAAA,CAAO,CAAC,OAAO,MAAMA,MAAA,QAAA,CAAQ,CAAA,GAAA,CAAA,K;MAGtB,OAAO,YAAlBL,UAAA,GAAAI,mBAA0F,OAA1F,YAA0FS,gBAAxB,OAAO,QAAQ,GAAA,CAAA,K;;I,WAEvE,CAAA,QAAA,MAAQ,UAApBb,UAAA,GAAAI,mBAA0F,OAA1F,YAA0FS,gBAA3BR,MAAA,QAAA,CAAQ,CAAC,SAAS,GAAA,CAAA,K"}
1
+ {"version":3,"file":"slash-menu.vue_vue_type_script_setup_true_lang.js","names":["_openBlock","_createElementBlock","_Fragment","_createVNode","_Transition","_withCtx","_unref","_normalizeStyle","_withModifiers","_renderList","_createElementVNode","_normalizeClass","_createBlock","_resolveDynamicComponent","_toDisplayString","_createCommentVNode","SlashChoices"],"sources":["../../src/components/slash-menu.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport { ChevronRightIcon } from '@dou-ui/icons'\nimport { isComposing, useFloating, useLayer, useZIndex, type VirtualElement } from 'dou-ui'\nimport { computed, inject, nextTick, shallowRef, useId, useTemplateRef, watch } from 'vue'\nimport { filterOptions } from '../catalog'\nimport { labelOf, type BlockOption } from '../core/extension'\nimport SlashChoices from './slash-choices.vue'\nimport { insertBlockAfter, setBlockType } from '../core/commands'\nimport { slashKey, type SlashState } from '../core/slash'\nimport { scrollParent } from '../dom'\nimport { editorKey } from '../editor'\n\nconst props = defineProps<{ slash: SlashState | null }>()\n\nconst { view, state, messages, kit } = inject(editorKey)!\nconst id = `dou-editor-slash-${useId()}`\nconst panel = useTemplateRef<HTMLElement>('panel')\nconst choicePanel = useTemplateRef<HTMLElement>('choicePanel')\nconst active = shallowRef(0)\n/**\n * The highlighted variant of the active option, when it has some (e.g. how\n * many columns); -1 while the option itself is highlighted, which inserts\n * the first variant.\n */\nconst choice = shallowRef(-1)\n/**\n * With a coarse pointer (a phone), the variants unfold in the menu under\n * their option, once it is tapped, instead of in a panel beside it that\n * would cover the menu on a narrow screen. Read when the menu opens.\n */\nconst coarse = shallowRef(false)\n/** The option whose variants are unfolded in the menu, or -1. */\nconst unfolded = shallowRef(-1)\n\nconst options = computed(() => {\n if (!props.slash) return []\n const current = state.value\n // only what can go here, e.g. no columns in a column\n const usable = current\n ? kit.blockOptions.filter((option) => setBlockType(option.type, option.props)(current))\n : kit.blockOptions\n return filterOptions(usable, props.slash.query, messages.value)\n})\nconst open = computed(() => !!props.slash)\nconst activeOption = computed(() => options.value[active.value])\nconst activeChoices = computed(() => (open.value ? activeOption.value?.choices : undefined))\n/** The variants in the panel beside the menu. */\nconst choices = computed(() => (coarse.value ? undefined : activeChoices.value))\n/** The variants unfolded in the menu, or shown beside it: where the keys pick from. */\nconst shownChoices = computed(() =>\n coarse.value\n ? unfolded.value === active.value\n ? activeChoices.value\n : undefined\n : choices.value,\n)\nwatch(activeOption, () => {\n // an option just unfolded keeps its variants\n if (unfolded.value === active.value) return\n choice.value = -1\n unfolded.value = -1\n})\nwatch(open, (value) => {\n if (value) coarse.value = window.matchMedia?.('(pointer: coarse)').matches ?? false\n})\n\n// the menu hangs below the `/` that opened it\nconst anchor: VirtualElement = {\n getBoundingClientRect() {\n const current = view.value\n const from = props.slash?.from\n if (!current || from === undefined) return new DOMRect()\n const { left, top, bottom } = current.coordsAtPos(from)\n return new DOMRect(left, top, 0, bottom - top)\n },\n}\nconst { style, placement, update } = useFloating(() => (open.value ? anchor : null), panel, {\n open,\n placement: 'bottom-start',\n offset: 6,\n})\n// the variants of the active option, in a panel beside it\nconst activeItem = () => (choices.value ? document.getElementById(`${id}-${active.value}`) : null)\nconst {\n style: choiceStyle,\n placement: choicePlacement,\n update: updateChoices,\n} = useFloating(activeItem, choicePanel, {\n open: () => !!choices.value,\n placement: 'right-start',\n offset: 4,\n})\nwatch([choices, active], () => void nextTick(updateChoices), { flush: 'post' })\n\n// stacked like DouUI's own popups, above whatever opened before\nconst { next } = useZIndex()\nconst zIndex = shallowRef(0)\nwatch(open, (value) => value && (zIndex.value = next()), { immediate: true })\n// a press outside the editor and the menu closes it; inside, the cursor move does\nuseLayer({\n open,\n elements: () => [panel.value, choicePanel.value, view.value?.dom],\n onOutside: () => close(),\n onEscape: () => close(),\n})\n\nwatch(\n () => props.slash?.query,\n () => {\n active.value = 0\n void nextTick(update)\n },\n)\n\n// the editor keeps focus; the active option, or variant, is announced through it\nwatch(\n [open, active, options, choice, shownChoices],\n () => {\n const dom = view.value?.dom\n if (!dom) return\n const option =\n open.value && options.value.length\n ? shownChoices.value && choice.value >= 0\n ? `${id}-choice-${choice.value}`\n : `${id}-${active.value}`\n : null\n if (open.value) {\n dom.setAttribute('aria-controls', choices.value ? `${id} ${id}-choices` : id)\n dom.setAttribute('aria-expanded', 'true')\n } else {\n dom.removeAttribute('aria-controls')\n dom.removeAttribute('aria-expanded')\n }\n if (option) dom.setAttribute('aria-activedescendant', option)\n else dom.removeAttribute('aria-activedescendant')\n if (option) document.getElementById(option)?.scrollIntoView({ block: 'nearest' })\n },\n { flush: 'post' },\n)\n\n/** On a touch screen, the first tap on an option with variants only shows them. */\nlet touched = false\nconst onItemPointer = (event: PointerEvent) => (touched = event.pointerType !== 'mouse')\n\nfunction onItemMove(event: PointerEvent, index: number) {\n if (event.pointerType === 'touch') return\n active.value = index\n // back on the option: clicking it inserts the first variant again\n choice.value = -1\n}\n\n/**\n * On a phone: the variants under option `index`, the first one lit (what\n * a tap on the option inserts), and in sight, the page scrolled up when\n * the menu has no room left below.\n */\nfunction unfold(index: number) {\n active.value = index\n unfolded.value = index\n choice.value = 0\n void nextTick(() => {\n const menu = panel.value\n const group = menu?.querySelector<HTMLElement>('.dou-editor-slash__unfolded')\n const dom = view.value?.dom\n if (!menu || !group || !dom) return\n const hidden = menu.scrollHeight - menu.clientHeight\n if (hidden > 0) {\n const scroller = scrollParent(dom)\n if (scroller) scroller.scrollBy({ top: hidden })\n else window.scrollBy({ top: hidden })\n }\n requestAnimationFrame(() => {\n update()\n requestAnimationFrame(() => group.scrollIntoView({ block: 'nearest' }))\n })\n })\n}\n\nfunction onItemClick(option: BlockOption, index: number) {\n if (option.choices && (coarse.value || touched) && unfolded.value !== index) {\n unfold(index)\n return\n }\n choose(option)\n}\n\nfunction choose(option: BlockOption, variant = option.choices ? Math.max(0, choice.value) : -1) {\n const current = view.value\n const slash = props.slash\n if (!current || !slash) return\n const chosen = { ...option.props, ...option.choices?.items[variant]?.props }\n // remove the `/query`, then turn the block, or add one below when it has text\n const removed = current.state.tr.delete(slash.from, current.state.selection.from)\n current.dispatch(removed.setMeta(slashKey, null))\n const { $from } = current.state.selection\n if ($from.parent.content.size > 0) insertBlockAfter($from.pos)(current.state, current.dispatch)\n if (option.type !== 'paragraph' || $from.parent.content.size === 0)\n setBlockType(option.type, chosen)(current.state, current.dispatch)\n current.focus()\n}\n\nfunction close() {\n const current = view.value\n if (current) current.dispatch(current.state.tr.setMeta(slashKey, null))\n}\n\n/** Keys the editor passes on while the menu is open. */\nfunction onKey(event: KeyboardEvent): boolean {\n if (isComposing(event)) return false\n const count = options.value.length\n switch (event.key) {\n case 'ArrowDown':\n case 'ArrowUp':\n if (!count) return false\n active.value = (active.value + (event.key === 'ArrowDown' ? 1 : -1) + count) % count\n return true\n case 'ArrowRight': {\n const items = activeChoices.value?.items\n if (!items) return false\n if (coarse.value && unfolded.value !== active.value) {\n unfold(active.value)\n return true\n }\n // past the last variant the key moves the cursor again\n if (choice.value >= items.length - 1) return false\n choice.value++\n return true\n }\n case 'ArrowLeft':\n // from the first variant back to the option; on the option the key moves the cursor\n if (!activeChoices.value || choice.value < 0) return false\n choice.value--\n return true\n case 'Enter':\n case 'Tab': {\n const option = options.value[active.value]\n if (!option) {\n close()\n return false\n }\n choose(option)\n return true\n }\n default:\n return false\n }\n}\n\ndefineExpose({ onKey })\n</script>\n\n<template>\n <Transition name=\"dou-floating\">\n <div\n v-if=\"open\"\n :id=\"id\"\n ref=\"panel\"\n role=\"listbox\"\n :aria-label=\"messages.blockTypes\"\n class=\"dou-dropdown dou-floating dou-editor-slash\"\n :style=\"[style, { zIndex }]\"\n :data-placement=\"placement\"\n @mousedown.prevent\n >\n <template v-for=\"(option, index) in options\" :key=\"option.key\">\n <div\n :id=\"`${id}-${index}`\"\n role=\"option\"\n :aria-selected=\"index === active\"\n :class=\"[\n 'dou-dropdown__item',\n 'dou-editor-slash__item',\n { 'is-active': index === active },\n ]\"\n @pointerdown=\"onItemPointer\"\n @pointermove=\"onItemMove($event, index)\"\n @click=\"onItemClick(option, index)\"\n >\n <span class=\"dou-editor-slash__icon\"><component :is=\"option.icon\" /></span>\n <span class=\"dou-editor-slash__text\">\n <span class=\"dou-editor-slash__label\">{{ labelOf(option.label, messages) }}</span>\n <span v-if=\"option.hint\" class=\"dou-editor-slash__hint\">{{\n labelOf(option.hint, messages)\n }}</span>\n </span>\n <kbd v-if=\"option.shortcut\" class=\"dou-editor-slash__shortcut\">{{ option.shortcut }}</kbd>\n <ChevronRightIcon\n v-if=\"option.choices\"\n :class=\"['dou-editor-slash__more', { 'is-unfolded': coarse && unfolded === index }]\"\n aria-hidden=\"true\"\n />\n </div>\n <!-- on a phone: the variants under their option -->\n <div\n v-if=\"option.choices && coarse && unfolded === index\"\n role=\"group\"\n :aria-label=\"labelOf(option.choices.title, messages)\"\n class=\"dou-editor-slash__unfolded\"\n >\n <SlashChoices\n :choices=\"option.choices\"\n :active=\"choice\"\n :messages=\"messages\"\n :id-prefix=\"`${id}-choice`\"\n @hover=\"choice = $event\"\n @choose=\"choose(option, $event)\"\n />\n </div>\n </template>\n <div v-if=\"!options.length\" class=\"dou-editor-slash__empty\">{{ messages.noResults }}</div>\n </div>\n </Transition>\n <Transition name=\"dou-floating\">\n <div\n v-if=\"choices && activeOption\"\n :id=\"`${id}-choices`\"\n ref=\"choicePanel\"\n role=\"listbox\"\n :aria-label=\"labelOf(choices.title, messages)\"\n class=\"dou-dropdown dou-floating dou-editor-slash__choices\"\n :style=\"[choiceStyle, { zIndex }]\"\n :data-placement=\"choicePlacement\"\n @mousedown.prevent\n >\n <SlashChoices\n :choices=\"choices\"\n :active=\"choice\"\n :messages=\"messages\"\n :id-prefix=\"`${id}-choice`\"\n @hover=\"choice = $event\"\n @choose=\"choose(activeOption!, $event)\"\n />\n </div>\n </Transition>\n</template>\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAYA,MAAM,QAAQ;EAEd,MAAM,EAAE,MAAM,OAAO,UAAU,QAAQ,OAAO,SAAS;EACvD,MAAM,KAAK,oBAAoB,MAAM;EACrC,MAAM,QAAQ,eAA4B,OAAO;EACjD,MAAM,cAAc,eAA4B,aAAa;EAC7D,MAAM,SAAS,WAAW,CAAC;;;;;;EAM3B,MAAM,SAAS,WAAW,EAAE;;;;;;EAM5B,MAAM,SAAS,WAAW,KAAK;;EAE/B,MAAM,WAAW,WAAW,EAAE;EAE9B,MAAM,UAAU,eAAe;GAC7B,IAAI,CAAC,MAAM,OAAO,OAAO,CAAC;GAC1B,MAAM,UAAU,MAAM;GAEtB,MAAM,SAAS,UACX,IAAI,aAAa,QAAQ,WAAW,aAAa,OAAO,MAAM,OAAO,KAAK,CAAC,CAAC,OAAO,CAAC,IACpF,IAAI;GACR,OAAO,cAAc,QAAQ,MAAM,MAAM,OAAO,SAAS,KAAK;EAChE,CAAC;EACD,MAAM,OAAO,eAAe,CAAC,CAAC,MAAM,KAAK;EACzC,MAAM,eAAe,eAAe,QAAQ,MAAM,OAAO,MAAM;EAC/D,MAAM,gBAAgB,eAAgB,KAAK,QAAQ,aAAa,OAAO,UAAU,KAAA,CAAU;;EAE3F,MAAM,UAAU,eAAgB,OAAO,QAAQ,KAAA,IAAY,cAAc,KAAM;;EA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A,QAAS;MAChB,WAAM,WAAE,OAAO,QAAQ,MAAM;;;;;;;;I,WAIxB,CAAA,QAAA,MAAQ,UAApBN,UAAA,GAAAC,mBAA0F,OAA1F,YAA0Fa,gBAA3BR,MAAA,QAAA,CAAQ,CAAC,SAAS,GAAA,CAAA,K;;G,IAGrFH,YAqBaC,YAAA,EArBD,MAAK,eAAc,GAAA;IAC7B,SAAAC,cAmBM,CAlBE,QAAA,SAAW,aAAA,SADnBL,UAAA,GAAAC,mBAmBM,OAAA;;KAjBH,IAAE,GAAK,GAAE;KACN,SAAA;KAAJ,KAAI;KACJ,MAAK;KACJ,cAAYK,MAAA,OAAA,CAAO,CAAC,QAAA,MAAQ,OAAOA,MAAA,QAAA,CAAQ;KAC5C,OAAM;KACL,OAAKC,eAAA,CAAGD,MAAA,WAAA,GAAW,EAAA,QAAI,OAAA,MAAM,CAAA,CAAA;KAC7B,kBAAgBA,MAAA,eAAA;KAChB,aAAS,OAAA,OAAA,OAAA,KAAAE,oBAAV,CAAA,GAAkB,CAAA,SAAA,CAAA;I,IAElBL,YAOEa,uBAAA;KANC,SAAS,QAAA;KACT,QAAQ,OAAA;KACR,UAAUV,MAAA,QAAA;KACV,aAAS,GAAK,GAAE;KAChB,SAAK,OAAA,OAAA,OAAA,MAAA,WAAE,OAAA,QAAS;KAChB,UAAM,OAAA,OAAA,OAAA,MAAA,WAAE,OAAO,aAAA,OAAe,MAAM"}
@@ -1,7 +1,8 @@
1
- import { blocksIn, findBlockById, selectedRange, siblingRangeBetween } from "./blocks.js";
1
+ import { blocksIn, findBlockById, layoutOf, parentBlock, selectedRange, siblingRangeBetween } from "./blocks.js";
2
2
  import { deleteBlocks, duplicateBlocks, liftBlocks, sinkBlocks, swapBlocks } from "./commands.js";
3
3
  import { keydownHandler } from "prosemirror-keymap";
4
4
  import { Plugin, PluginKey, TextSelection } from "prosemirror-state";
5
+ import { Fragment, Slice } from "prosemirror-model";
5
6
  import { Decoration, DecorationSet } from "prosemirror-view";
6
7
  // src/core/block-selection.ts
7
8
  var blockSelectionKey = new PluginKey("dou-editor-block-selection");
@@ -75,7 +76,12 @@ function moveHead(view, direction, extend) {
75
76
  if (!range) return false;
76
77
  const head = findBlockById(state.doc, current.head);
77
78
  const index = state.doc.resolve(head.pos).index() + direction;
78
- if (index < 0 || index >= range.parent.childCount) return true;
79
+ if (index < 0 || index >= range.parent.childCount) {
80
+ const column = parentBlock(state.doc, head);
81
+ const row = column && layoutOf(column.node) === "column" && parentBlock(state.doc, column);
82
+ if (row) selectBlocks(row.node.attrs.id)(state, view.dispatch);
83
+ return true;
84
+ }
79
85
  const id = range.parent.child(index).attrs.id;
80
86
  return selectBlocks(extend ? current.anchor : id, id)(state, view.dispatch);
81
87
  }
@@ -115,6 +121,24 @@ var blockKeys = keydownHandler({
115
121
  return true;
116
122
  }
117
123
  });
124
+ /** Put the selected blocks on the clipboard, and remove them when cutting. */
125
+ function copyBlocks(view, event, cut) {
126
+ const range = blockSelectionRange(view.state);
127
+ const data = event.clipboardData;
128
+ if (!range || !data) return false;
129
+ const blocks = blocksIn(range);
130
+ const slice = new Slice(Fragment.from(blocks.map((block) => block.node)), 0, 0);
131
+ const { dom, text } = view.serializeForClipboard(slice);
132
+ event.preventDefault();
133
+ data.clearData();
134
+ data.setData("text/html", dom.innerHTML);
135
+ data.setData("text/plain", text);
136
+ if (cut && view.editable) {
137
+ const tr = deleteBlocks(view.state.tr, blocks).setMeta(blockSelectionKey, null);
138
+ view.dispatch(tr.scrollIntoView().setMeta("uiEvent", "cut"));
139
+ }
140
+ return true;
141
+ }
118
142
  function blockSelectionPlugin() {
119
143
  return new Plugin({
120
144
  key: blockSelectionKey,
@@ -123,15 +147,19 @@ function blockSelectionPlugin() {
123
147
  apply(tr, value) {
124
148
  const meta = tr.getMeta(blockSelectionKey);
125
149
  if (meta !== void 0) return meta;
126
- if (value && tr.selectionSet && !tr.docChanged) return null;
150
+ if (value && tr.selectionSet && !tr.docChanged && !tr.getMeta("appendedTransaction")) return null;
127
151
  return value;
128
152
  }
129
153
  },
130
154
  props: {
155
+ handleDOMEvents: {
156
+ copy: (view, event) => copyBlocks(view, event, false),
157
+ cut: (view, event) => copyBlocks(view, event, true)
158
+ },
131
159
  decorations(state) {
132
160
  const range = blockSelectionRange(state);
133
161
  if (!range) return null;
134
- return DecorationSet.create(state.doc, blocksIn(range).map((block) => Decoration.node(block.pos, block.pos + block.node.nodeSize, { class: "is-selected" })));
162
+ return DecorationSet.create(state.doc, blocksIn(range).map((block) => Decoration.node(block.pos, block.pos + block.node.nodeSize, { class: block.node.firstChild?.type.spec.tableRole === "table" ? "is-selected has-table" : "is-selected" })));
135
163
  },
136
164
  attributes(state) {
137
165
  return getBlockSelection(state) ? { class: "is-block-selecting" } : {};
@@ -1 +1 @@
1
- {"version":3,"file":"block-selection.js","names":[],"sources":["../../src/core/block-selection.ts"],"sourcesContent":["import { keydownHandler } from 'prosemirror-keymap'\nimport {\n Plugin,\n PluginKey,\n TextSelection,\n type Command,\n type EditorState,\n type Transaction,\n} from 'prosemirror-state'\nimport { Decoration, DecorationSet, type EditorView } from 'prosemirror-view'\nimport {\n blocksIn,\n findBlockById,\n selectedRange,\n siblingRangeBetween,\n type BlockInfo,\n type SiblingRange,\n} from './blocks'\nimport { duplicateBlocks, liftBlocks, swapBlocks, deleteBlocks, sinkBlocks } from './commands'\n\n/**\n * Whole blocks selected with `Esc` or from the block handle. The selected\n * blocks are siblings, from `anchor` to `head`. Underneath, the editor's\n * own selection spans their text, so copy and cut work as usual; the text\n * highlight is hidden and the blocks are tinted instead.\n */\nexport interface BlockSelection {\n anchor: string\n head: string\n}\n\nexport const blockSelectionKey = new PluginKey<BlockSelection | null>('dou-editor-block-selection')\n\nexport function getBlockSelection(state: EditorState): BlockSelection | null {\n return blockSelectionKey.getState(state) ?? null\n}\n\n/** The blocks of a block selection, or `null` when they can't be found. */\nexport function blockSelectionRange(state: EditorState): SiblingRange | null {\n const current = getBlockSelection(state)\n if (!current) return null\n const anchor = findBlockById(state.doc, current.anchor)\n const head = findBlockById(state.doc, current.head)\n if (!anchor || !head) return null\n const [first, last] = anchor.pos <= head.pos ? [anchor, head] : [head, anchor]\n const range = siblingRangeBetween(state.doc, first.pos + 1, last.pos + 1)\n return range && range.depth === anchor.depth ? range : null\n}\n\n/** Put the editor's own selection over the text of the selected blocks. */\nfunction spanSelection(tr: Transaction, range: SiblingRange) {\n const blocks = blocksIn(range)\n const first = blocks[0]!\n const last = blocks.at(-1)!\n const $from = tr.doc.resolve(first.contentPos + 1)\n const $to = tr.doc.resolve(last.pos + last.node.nodeSize - 1)\n tr.setSelection(TextSelection.between($from, $to))\n}\n\n/** Select whole blocks, from `anchor` to `head` (siblings). */\nexport function selectBlocks(anchor: string, head = anchor): Command {\n return (state, dispatch) => {\n const tr = state.tr.setMeta(blockSelectionKey, { anchor, head })\n const preview = state.apply(tr)\n const range = blockSelectionRange(preview)\n if (!range) return false\n if (dispatch) {\n spanSelection(tr, range)\n tr.setMeta(blockSelectionKey, { anchor, head })\n dispatch(tr.scrollIntoView())\n }\n return true\n }\n}\n\nexport const clearBlockSelection: Command = (state, dispatch) => {\n if (!getBlockSelection(state)) return false\n if (dispatch) dispatch(state.tr.setMeta(blockSelectionKey, null))\n return true\n}\n\n/** `Esc`: select the blocks the cursor or text selection is in. */\nexport const selectCurrentBlocks: Command = (state, dispatch) => {\n if (getBlockSelection(state)) return false\n const range = selectedRange(state)\n if (!range) return false\n const blocks = blocksIn(range)\n return selectBlocks(blocks[0]!.node.attrs.id, blocks.at(-1)!.node.attrs.id)(state, dispatch)\n}\n\n/** Leave the block selection with the cursor at the end of a block. */\nfunction placeCursor(view: EditorView, block: BlockInfo, atEnd = true) {\n const tr = view.state.tr.setMeta(blockSelectionKey, null)\n const pos = atEnd ? block.contentPos + block.content.nodeSize - 1 : block.contentPos + 1\n tr.setSelection(TextSelection.near(tr.doc.resolve(pos), atEnd ? -1 : 1))\n view.dispatch(tr.scrollIntoView())\n}\n\nfunction moveHead(view: EditorView, direction: -1 | 1, extend: boolean) {\n const state = view.state\n const current = getBlockSelection(state)!\n const range = blockSelectionRange(state)\n if (!range) return false\n const head = findBlockById(state.doc, current.head)!\n const $head = state.doc.resolve(head.pos)\n const index = $head.index() + direction\n if (index < 0 || index >= range.parent.childCount) return true\n const id = range.parent.child(index).attrs.id as string\n return selectBlocks(extend ? current.anchor : id, id)(state, view.dispatch)\n}\n\n/** Run a block command, then select the same blocks again. */\nfunction keepSelected(command: Command): (view: EditorView) => boolean {\n return (view) => {\n const current = getBlockSelection(view.state)\n if (!current || !command(view.state, view.dispatch)) return false\n selectBlocks(current.anchor, current.head)(view.state, view.dispatch)\n return true\n }\n}\n\nconst removeSelected = (view: EditorView) => {\n const range = blockSelectionRange(view.state)\n if (!range) return false\n const tr = deleteBlocks(view.state.tr, blocksIn(range)).setMeta(blockSelectionKey, null)\n view.dispatch(tr.scrollIntoView())\n return true\n}\n\nconst blockKeys = keydownHandler({\n ArrowUp: (_state, _dispatch, view) => moveHead(view!, -1, false),\n ArrowDown: (_state, _dispatch, view) => moveHead(view!, 1, false),\n 'Shift-ArrowUp': (_state, _dispatch, view) => moveHead(view!, -1, true),\n 'Shift-ArrowDown': (_state, _dispatch, view) => moveHead(view!, 1, true),\n Backspace: (_state, _dispatch, view) => removeSelected(view!),\n Delete: (_state, _dispatch, view) => removeSelected(view!),\n 'Mod-d': (_state, _dispatch, view) => keepSelected(duplicateBlocks)(view!),\n 'Mod-Shift-ArrowUp': (_state, _dispatch, view) => keepSelected(swapBlocks(-1))(view!),\n 'Mod-Shift-ArrowDown': (_state, _dispatch, view) => keepSelected(swapBlocks(1))(view!),\n Tab: (_state, _dispatch, view) => keepSelected(sinkBlocks)(view!),\n 'Shift-Tab': (_state, _dispatch, view) => keepSelected(liftBlocks)(view!),\n Escape: (state, dispatch) => clearBlockSelection(state, dispatch),\n Enter: (state, _dispatch, view) => {\n const current = getBlockSelection(state)!\n const head = findBlockById(state.doc, current.head)\n if (head) placeCursor(view!, head)\n return true\n },\n})\n\nexport function blockSelectionPlugin(): Plugin<BlockSelection | null> {\n return new Plugin<BlockSelection | null>({\n key: blockSelectionKey,\n state: {\n init: () => null,\n apply(tr, value) {\n const meta = tr.getMeta(blockSelectionKey) as BlockSelection | null | undefined\n if (meta !== undefined) return meta\n // any other selection change (a click, typing) ends it\n if (value && tr.selectionSet && !tr.docChanged) return null\n return value\n },\n },\n props: {\n decorations(state) {\n const range = blockSelectionRange(state)\n if (!range) return null\n return DecorationSet.create(\n state.doc,\n blocksIn(range).map((block) =>\n Decoration.node(block.pos, block.pos + block.node.nodeSize, { class: 'is-selected' }),\n ),\n )\n },\n attributes(state): Record<string, string> {\n return getBlockSelection(state) ? { class: 'is-block-selecting' } : {}\n },\n handleKeyDown(view, event) {\n const current = getBlockSelection(view.state)\n if (!current) return false\n if (blockKeys(view, event)) return true\n // typing a character: back to text editing at the end of the head block\n if (event.key.length === 1 && !event.metaKey && !event.ctrlKey && !event.altKey) {\n const head = findBlockById(view.state.doc, current.head)\n if (head) placeCursor(view, head)\n return false\n }\n return false\n },\n },\n })\n}\n"],"mappings":";;;;;;AA+BA,IAAa,oBAAoB,IAAI,UAAiC,4BAA4B;AAElG,SAAgB,kBAAkB,OAA2C;CAC3E,OAAO,kBAAkB,SAAS,KAAK,KAAK;AAC9C;;AAGA,SAAgB,oBAAoB,OAAyC;CAC3E,MAAM,UAAU,kBAAkB,KAAK;CACvC,IAAI,CAAC,SAAS,OAAO;CACrB,MAAM,SAAS,cAAc,MAAM,KAAK,QAAQ,MAAM;CACtD,MAAM,OAAO,cAAc,MAAM,KAAK,QAAQ,IAAI;CAClD,IAAI,CAAC,UAAU,CAAC,MAAM,OAAO;CAC7B,MAAM,CAAC,OAAO,QAAQ,OAAO,OAAO,KAAK,MAAM,CAAC,QAAQ,IAAI,IAAI,CAAC,MAAM,MAAM;CAC7E,MAAM,QAAQ,oBAAoB,MAAM,KAAK,MAAM,MAAM,GAAG,KAAK,MAAM,CAAC;CACxE,OAAO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,oBAAoB,KAAK;IACvC,IAAI,CAAC,OAAO,OAAO;IACnB,OAAO,cAAc,OACnB,MAAM,KACN,SAAS,KAAK,CAAC,CAAC,KAAK,UACnB,WAAW,KAAK,MAAM,KAAK,MAAM,MAAM,MAAM,KAAK,UAAU,EAAE,OAAO,cAAc,CAAC,CACtF,CACF;GACF;GACA,WAAW,OAA+B;IACxC,OAAO,kBAAkB,KAAK,IAAI,EAAE,OAAO,qBAAqB,IAAI,CAAC;GACvE;GACA,cAAc,MAAM,OAAO;IACzB,MAAM,UAAU,kBAAkB,KAAK,KAAK;IAC5C,IAAI,CAAC,SAAS,OAAO;IACrB,IAAI,UAAU,MAAM,KAAK,GAAG,OAAO;IAEnC,IAAI,MAAM,IAAI,WAAW,KAAK,CAAC,MAAM,WAAW,CAAC,MAAM,WAAW,CAAC,MAAM,QAAQ;KAC/E,MAAM,OAAO,cAAc,KAAK,MAAM,KAAK,QAAQ,IAAI;KACvD,IAAI,MAAM,YAAY,MAAM,IAAI;KAChC,OAAO;IACT;IACA,OAAO;GACT;EACF;CACF,CAAC;AACH"}
1
+ {"version":3,"file":"block-selection.js","names":[],"sources":["../../src/core/block-selection.ts"],"sourcesContent":["import { keydownHandler } from 'prosemirror-keymap'\nimport { Fragment, Slice } from 'prosemirror-model'\nimport {\n Plugin,\n PluginKey,\n TextSelection,\n type Command,\n type EditorState,\n type Transaction,\n} from 'prosemirror-state'\nimport { Decoration, DecorationSet, type EditorView } from 'prosemirror-view'\nimport {\n blocksIn,\n findBlockById,\n layoutOf,\n parentBlock,\n selectedRange,\n siblingRangeBetween,\n type BlockInfo,\n type SiblingRange,\n} from './blocks'\nimport { duplicateBlocks, liftBlocks, swapBlocks, deleteBlocks, sinkBlocks } from './commands'\n\n/**\n * Whole blocks selected with `Esc` or from the block handle. The selected\n * blocks are siblings, from `anchor` to `head`. Underneath, the editor's\n * own selection spans their text, so copy and cut work as usual; the text\n * highlight is hidden and the blocks are tinted instead.\n */\nexport interface BlockSelection {\n anchor: string\n head: string\n}\n\nexport const blockSelectionKey = new PluginKey<BlockSelection | null>('dou-editor-block-selection')\n\nexport function getBlockSelection(state: EditorState): BlockSelection | null {\n return blockSelectionKey.getState(state) ?? null\n}\n\n/** The blocks of a block selection, or `null` when they can't be found. */\nexport function blockSelectionRange(state: EditorState): SiblingRange | null {\n const current = getBlockSelection(state)\n if (!current) return null\n const anchor = findBlockById(state.doc, current.anchor)\n const head = findBlockById(state.doc, current.head)\n if (!anchor || !head) return null\n const [first, last] = anchor.pos <= head.pos ? [anchor, head] : [head, anchor]\n const range = siblingRangeBetween(state.doc, first.pos + 1, last.pos + 1)\n return range && range.depth === anchor.depth ? range : null\n}\n\n/** Put the editor's own selection over the text of the selected blocks. */\nfunction spanSelection(tr: Transaction, range: SiblingRange) {\n const blocks = blocksIn(range)\n const first = blocks[0]!\n const last = blocks.at(-1)!\n const $from = tr.doc.resolve(first.contentPos + 1)\n const $to = tr.doc.resolve(last.pos + last.node.nodeSize - 1)\n tr.setSelection(TextSelection.between($from, $to))\n}\n\n/** Select whole blocks, from `anchor` to `head` (siblings). */\nexport function selectBlocks(anchor: string, head = anchor): Command {\n return (state, dispatch) => {\n const tr = state.tr.setMeta(blockSelectionKey, { anchor, head })\n const preview = state.apply(tr)\n const range = blockSelectionRange(preview)\n if (!range) return false\n if (dispatch) {\n spanSelection(tr, range)\n tr.setMeta(blockSelectionKey, { anchor, head })\n dispatch(tr.scrollIntoView())\n }\n return true\n }\n}\n\nexport const clearBlockSelection: Command = (state, dispatch) => {\n if (!getBlockSelection(state)) return false\n if (dispatch) dispatch(state.tr.setMeta(blockSelectionKey, null))\n return true\n}\n\n/** `Esc`: select the blocks the cursor or text selection is in. */\nexport const selectCurrentBlocks: Command = (state, dispatch) => {\n if (getBlockSelection(state)) return false\n const range = selectedRange(state)\n if (!range) return false\n const blocks = blocksIn(range)\n return selectBlocks(blocks[0]!.node.attrs.id, blocks.at(-1)!.node.attrs.id)(state, dispatch)\n}\n\n/** Leave the block selection with the cursor at the end of a block. */\nfunction placeCursor(view: EditorView, block: BlockInfo, atEnd = true) {\n const tr = view.state.tr.setMeta(blockSelectionKey, null)\n const pos = atEnd ? block.contentPos + block.content.nodeSize - 1 : block.contentPos + 1\n tr.setSelection(TextSelection.near(tr.doc.resolve(pos), atEnd ? -1 : 1))\n view.dispatch(tr.scrollIntoView())\n}\n\nfunction moveHead(view: EditorView, direction: -1 | 1, extend: boolean) {\n const state = view.state\n const current = getBlockSelection(state)!\n const range = blockSelectionRange(state)\n if (!range) return false\n const head = findBlockById(state.doc, current.head)!\n const $head = state.doc.resolve(head.pos)\n const index = $head.index() + direction\n if (index < 0 || index >= range.parent.childCount) {\n // past the edge of a column: the whole set of columns\n const column = parentBlock(state.doc, head)\n const row = column && layoutOf(column.node) === 'column' && parentBlock(state.doc, column)\n if (row) selectBlocks(row.node.attrs.id as string)(state, view.dispatch)\n return true\n }\n const id = range.parent.child(index).attrs.id as string\n return selectBlocks(extend ? current.anchor : id, id)(state, view.dispatch)\n}\n\n/** Run a block command, then select the same blocks again. */\nfunction keepSelected(command: Command): (view: EditorView) => boolean {\n return (view) => {\n const current = getBlockSelection(view.state)\n if (!current || !command(view.state, view.dispatch)) return false\n selectBlocks(current.anchor, current.head)(view.state, view.dispatch)\n return true\n }\n}\n\nconst removeSelected = (view: EditorView) => {\n const range = blockSelectionRange(view.state)\n if (!range) return false\n const tr = deleteBlocks(view.state.tr, blocksIn(range)).setMeta(blockSelectionKey, null)\n view.dispatch(tr.scrollIntoView())\n return true\n}\n\nconst blockKeys = keydownHandler({\n ArrowUp: (_state, _dispatch, view) => moveHead(view!, -1, false),\n ArrowDown: (_state, _dispatch, view) => moveHead(view!, 1, false),\n 'Shift-ArrowUp': (_state, _dispatch, view) => moveHead(view!, -1, true),\n 'Shift-ArrowDown': (_state, _dispatch, view) => moveHead(view!, 1, true),\n Backspace: (_state, _dispatch, view) => removeSelected(view!),\n Delete: (_state, _dispatch, view) => removeSelected(view!),\n 'Mod-d': (_state, _dispatch, view) => keepSelected(duplicateBlocks)(view!),\n 'Mod-Shift-ArrowUp': (_state, _dispatch, view) => keepSelected(swapBlocks(-1))(view!),\n 'Mod-Shift-ArrowDown': (_state, _dispatch, view) => keepSelected(swapBlocks(1))(view!),\n Tab: (_state, _dispatch, view) => keepSelected(sinkBlocks)(view!),\n 'Shift-Tab': (_state, _dispatch, view) => keepSelected(liftBlocks)(view!),\n Escape: (state, dispatch) => clearBlockSelection(state, dispatch),\n Enter: (state, _dispatch, view) => {\n const current = getBlockSelection(state)!\n const head = findBlockById(state.doc, current.head)\n if (head) placeCursor(view!, head)\n return true\n },\n})\n\n/** Put the selected blocks on the clipboard, and remove them when cutting. */\nfunction copyBlocks(view: EditorView, event: ClipboardEvent, cut: boolean): boolean {\n const range = blockSelectionRange(view.state)\n const data = event.clipboardData\n if (!range || !data) return false\n const blocks = blocksIn(range)\n const slice = new Slice(Fragment.from(blocks.map((block) => block.node)), 0, 0)\n const { dom, text } = view.serializeForClipboard(slice)\n event.preventDefault()\n data.clearData()\n data.setData('text/html', dom.innerHTML)\n data.setData('text/plain', text)\n if (cut && view.editable) {\n const tr = deleteBlocks(view.state.tr, blocks).setMeta(blockSelectionKey, null)\n view.dispatch(tr.scrollIntoView().setMeta('uiEvent', 'cut'))\n }\n return true\n}\n\nexport function blockSelectionPlugin(): Plugin<BlockSelection | null> {\n return new Plugin<BlockSelection | null>({\n key: blockSelectionKey,\n state: {\n init: () => null,\n apply(tr, value) {\n const meta = tr.getMeta(blockSelectionKey) as BlockSelection | null | undefined\n if (meta !== undefined) return meta\n // any other selection change (a click, typing) ends it; not a plugin\n // adjusting the selection under it (a table's turns into cells)\n if (value && tr.selectionSet && !tr.docChanged && !tr.getMeta('appendedTransaction'))\n return null\n return value\n },\n },\n props: {\n // copy and cut take the whole blocks, not the text under them (a\n // table's would be one cell)\n handleDOMEvents: {\n copy: (view, event) => copyBlocks(view, event, false),\n cut: (view, event) => copyBlocks(view, event, true),\n },\n decorations(state) {\n const range = blockSelectionRange(state)\n if (!range) return null\n return DecorationSet.create(\n state.doc,\n blocksIn(range).map((block) =>\n Decoration.node(block.pos, block.pos + block.node.nodeSize, {\n // a top-level table reaches into the margins: its tint goes with it\n class:\n block.node.firstChild?.type.spec.tableRole === 'table'\n ? 'is-selected has-table'\n : 'is-selected',\n }),\n ),\n )\n },\n attributes(state): Record<string, string> {\n return getBlockSelection(state) ? { class: 'is-block-selecting' } : {}\n },\n handleKeyDown(view, event) {\n const current = getBlockSelection(view.state)\n if (!current) return false\n if (blockKeys(view, event)) return true\n // typing a character: back to text editing at the end of the head block\n if (event.key.length === 1 && !event.metaKey && !event.ctrlKey && !event.altKey) {\n const head = findBlockById(view.state.doc, current.head)\n if (head) placeCursor(view, head)\n return false\n }\n return false\n },\n },\n })\n}\n"],"mappings":";;;;;;;AAkCA,IAAa,oBAAoB,IAAI,UAAiC,4BAA4B;AAElG,SAAgB,kBAAkB,OAA2C;CAC3E,OAAO,kBAAkB,SAAS,KAAK,KAAK;AAC9C;;AAGA,SAAgB,oBAAoB,OAAyC;CAC3E,MAAM,UAAU,kBAAkB,KAAK;CACvC,IAAI,CAAC,SAAS,OAAO;CACrB,MAAM,SAAS,cAAc,MAAM,KAAK,QAAQ,MAAM;CACtD,MAAM,OAAO,cAAc,MAAM,KAAK,QAAQ,IAAI;CAClD,IAAI,CAAC,UAAU,CAAC,MAAM,OAAO;CAC7B,MAAM,CAAC,OAAO,QAAQ,OAAO,OAAO,KAAK,MAAM,CAAC,QAAQ,IAAI,IAAI,CAAC,MAAM,MAAM;CAC7E,MAAM,QAAQ,oBAAoB,MAAM,KAAK,MAAM,MAAM,GAAG,KAAK,MAAM,CAAC;CACxE,OAAO,SAAS,MAAM,UAAU,OAAO,QAAQ,QAAQ;AACzD;;AAGA,SAAS,cAAc,IAAiB,OAAqB;CAC3D,MAAM,SAAS,SAAS,KAAK;CAC7B,MAAM,QAAQ,OAAO;CACrB,MAAM,OAAO,OAAO,GAAG,EAAE;CAC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AC5C,IAAI,CAAC,SAAS,OAAO;IACrB,IAAI,UAAU,MAAM,KAAK,GAAG,OAAO;IAEnC,IAAI,MAAM,IAAI,WAAW,KAAK,CAAC,MAAM,WAAW,CAAC,MAAM,WAAW,CAAC,MAAM,QAAQ;KAC/E,MAAM,OAAO,cAAc,KAAK,MAAM,KAAK,QAAQ,IAAI;KACvD,IAAI,MAAM,YAAY,MAAM,IAAI;KAChC,OAAO;IACT;IACA,OAAO;GACT;EACF;CACF,CAAC;AACH"}
@@ -1,4 +1,5 @@
1
1
  // src/core/blocks.ts
2
+ var layoutOf = (block) => block?.firstChild?.type.spec.layout;
2
3
  function infoAt(node, pos, depth) {
3
4
  return {
4
5
  node,
@@ -19,6 +20,12 @@ function blockAround($pos) {
19
20
  if (after?.type.name === "block") return infoAt(after, $pos.pos, $pos.depth + 1);
20
21
  return null;
21
22
  }
23
+ /** The block whose children `info` is one of, or `null` at the top level. */
24
+ function parentBlock(doc, info) {
25
+ if (info.depth <= 1) return null;
26
+ const $pos = doc.resolve(info.pos);
27
+ return infoAt($pos.node(info.depth - 2), $pos.before(info.depth - 2), info.depth - 2);
28
+ }
22
29
  /** The block with an id, by scanning the document. */
23
30
  function findBlockById(doc, id) {
24
31
  let found = null;
@@ -66,6 +73,11 @@ function siblingRangeBetween(doc, from, to) {
66
73
  if (doc.resolve(a.pos).start() === doc.resolve(b.pos).start()) break;
67
74
  depth -= 2;
68
75
  }
76
+ if (depth > 1) {
77
+ const $a = doc.resolve(a.pos);
78
+ const owner = $a.node(depth - 2);
79
+ if (layoutOf(owner) === "row") a = b = infoAt(owner, $a.before(depth - 2), depth - 2);
80
+ }
69
81
  return rangeOf(doc, a, b);
70
82
  }
71
83
  function selectedRange(state) {
@@ -112,7 +124,11 @@ function previousInDocument(doc, info) {
112
124
  const before = $pos.parent.child(index - 1);
113
125
  return lastDescendant(infoAt(before, info.pos - before.nodeSize, info.depth));
114
126
  }
115
- if (info.depth > 1) return infoAt($pos.node(info.depth - 2), $pos.before(info.depth - 2), info.depth - 2);
127
+ if (info.depth > 1) {
128
+ const parent = $pos.node(info.depth - 2);
129
+ if (layoutOf(parent) === "column") return null;
130
+ return infoAt(parent, $pos.before(info.depth - 2), info.depth - 2);
131
+ }
116
132
  return null;
117
133
  }
118
134
  /** The block that comes after `info` when reading top to bottom. */
@@ -124,9 +140,11 @@ function nextInDocument(doc, info) {
124
140
  const end = current.pos + current.node.nodeSize;
125
141
  if ($pos.index() < $pos.parent.childCount - 1) return infoAt($pos.parent.child($pos.index() + 1), end, current.depth);
126
142
  if (current.depth <= 1) return null;
127
- current = infoAt($pos.node(current.depth - 2), $pos.before(current.depth - 2), current.depth - 2);
143
+ const parent = $pos.node(current.depth - 2);
144
+ if (layoutOf(parent) === "column") return null;
145
+ current = infoAt(parent, $pos.before(current.depth - 2), current.depth - 2);
128
146
  }
129
147
  }
130
- export { allBlocks, blockAround, blocksIn, findBlockById, infoAt, lastDescendant, nextInDocument, previousInDocument, selectedRange, siblingRangeBetween };
148
+ export { allBlocks, blockAround, blocksIn, findBlockById, infoAt, lastDescendant, layoutOf, nextInDocument, parentBlock, previousInDocument, selectedRange, siblingRangeBetween };
131
149
 
132
150
  //# sourceMappingURL=blocks.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"blocks.js","names":[],"sources":["../../src/core/blocks.ts"],"sourcesContent":["import type { Node as PMNode, ResolvedPos } from 'prosemirror-model'\nimport type { EditorState } from 'prosemirror-state'\n\n/** A `block` node and where it sits. */\nexport interface BlockInfo {\n node: PMNode\n /** The position right before the block. */\n pos: number\n /** Its depth in the document; top-level blocks are at depth 1. */\n depth: number\n /** The block's content node (paragraph, heading, …). */\n content: PMNode\n /** The position right before the content node. */\n contentPos: number\n /** The children, if any. */\n group: PMNode | null\n}\n\nexport function infoAt(node: PMNode, pos: number, depth: number): BlockInfo {\n return {\n node,\n pos,\n depth,\n content: node.firstChild!,\n contentPos: pos + 1,\n group: node.childCount > 1 ? node.child(1) : null,\n }\n}\n\n/** The innermost block around a position, or `null` between blocks. */\nexport function blockAround($pos: ResolvedPos): BlockInfo | null {\n for (let depth = $pos.depth; depth > 0; depth--) {\n const node = $pos.node(depth)\n if (node.type.name === 'block') return infoAt(node, $pos.before(depth), depth)\n }\n // right before a block, e.g. a node selection of it\n const after = $pos.nodeAfter\n if (after?.type.name === 'block') return infoAt(after, $pos.pos, $pos.depth + 1)\n return null\n}\n\n/** The block with an id, by scanning the document. */\nexport function findBlockById(doc: PMNode, id: string): BlockInfo | null {\n let found: BlockInfo | null = null\n doc.descendants((node, pos) => {\n if (found) return false\n if (node.type.name === 'block' && node.attrs.id === id) {\n found = infoAt(node, pos, doc.resolve(pos).depth + 1)\n return false\n }\n // blocks only live in the doc and in groups; skip inline content\n return node.type.name === 'block' || node.type.name === 'blockGroup'\n })\n return found\n}\n\n/** A run of sibling blocks inside one container (the doc or a group). */\nexport interface SiblingRange {\n /** The container: the doc or a `blockGroup`. */\n parent: PMNode\n /** The position where the container's content starts. */\n parentStart: number\n /** Depth of the blocks. */\n depth: number\n /** First and last index, inclusive. */\n from: number\n to: number\n /** Positions before the first and after the last block. */\n start: number\n end: number\n}\n\nfunction rangeOf(doc: PMNode, a: BlockInfo, b: BlockInfo): SiblingRange {\n const $a = doc.resolve(a.pos)\n const $b = doc.resolve(b.pos)\n const parent = $a.parent\n return {\n parent,\n parentStart: $a.start(),\n depth: a.depth,\n from: $a.index(),\n to: $b.index(),\n start: a.pos,\n end: b.pos + b.node.nodeSize,\n }\n}\n\n/** The outer block at `depth` that contains `info`. */\nfunction climb(doc: PMNode, info: BlockInfo, depth: number): BlockInfo {\n if (info.depth === depth) return info\n const $pos = doc.resolve(info.pos + 1)\n return infoAt($pos.node(depth), $pos.before(depth), depth)\n}\n\n/**\n * The sibling blocks a selection spans. When its ends sit at different\n * levels, the range climbs to the level they share.\n */\nexport function siblingRangeBetween(doc: PMNode, from: number, to: number): SiblingRange | null {\n let a = blockAround(doc.resolve(from))\n let b = blockAround(doc.resolve(to))\n if (!a || !b) return null\n // climb two levels at a time (block > group > block) until both share a parent\n let depth = Math.min(a.depth, b.depth)\n for (;;) {\n a = climb(doc, a, depth)\n b = climb(doc, b, depth)\n if (doc.resolve(a.pos).start() === doc.resolve(b.pos).start()) break\n depth -= 2\n }\n return rangeOf(doc, a, b)\n}\n\nexport function selectedRange(state: EditorState): SiblingRange | null {\n const { from, to } = state.selection\n return siblingRangeBetween(state.doc, from, to)\n}\n\n/** The blocks of a range, in order. */\nexport function blocksIn(range: SiblingRange): BlockInfo[] {\n const list: BlockInfo[] = []\n let pos = range.parentStart\n range.parent.forEach((node, offset, index) => {\n if (index >= range.from && index <= range.to) list.push(infoAt(node, pos + offset, range.depth))\n })\n return list\n}\n\n/** Every block in document order, outer before inner. */\nexport function allBlocks(doc: PMNode): BlockInfo[] {\n const list: BlockInfo[] = []\n // walked by hand: resolving every position would scan the document again each time\n const visit = (container: PMNode, start: number, depth: number) => {\n container.forEach((block, offset) => {\n const pos = start + offset\n list.push(infoAt(block, pos, depth))\n if (block.childCount > 1)\n visit(block.child(1), pos + 1 + block.firstChild!.nodeSize + 1, depth + 2)\n })\n }\n visit(doc, 0, 1)\n return list\n}\n\n/** The deepest last block inside a block (itself when it has no children). */\nexport function lastDescendant(info: BlockInfo): BlockInfo {\n let current = info\n while (current.group) {\n const group = current.group\n const lastPos =\n current.pos +\n 1 +\n current.content.nodeSize +\n 1 +\n group.content.size -\n group.lastChild!.nodeSize\n current = infoAt(group.lastChild!, lastPos, current.depth + 2)\n }\n return current\n}\n\n/** The block that comes before `info` when reading top to bottom. */\nexport function previousInDocument(doc: PMNode, info: BlockInfo): BlockInfo | null {\n const $pos = doc.resolve(info.pos)\n const index = $pos.index()\n if (index > 0) {\n const before = $pos.parent.child(index - 1)\n return lastDescendant(infoAt(before, info.pos - before.nodeSize, info.depth))\n }\n // the first child: the previous is its parent block\n if (info.depth > 1)\n return infoAt($pos.node(info.depth - 2), $pos.before(info.depth - 2), info.depth - 2)\n return null\n}\n\n/** The block that comes after `info` when reading top to bottom. */\nexport function nextInDocument(doc: PMNode, info: BlockInfo): BlockInfo | null {\n if (info.group)\n return infoAt(info.group.firstChild!, info.pos + 1 + info.content.nodeSize + 1, info.depth + 2)\n let current = info\n for (;;) {\n const $pos = doc.resolve(current.pos)\n const end = current.pos + current.node.nodeSize\n if ($pos.index() < $pos.parent.childCount - 1)\n return infoAt($pos.parent.child($pos.index() + 1), end, current.depth)\n if (current.depth <= 1) return null\n current = infoAt(\n $pos.node(current.depth - 2),\n $pos.before(current.depth - 2),\n current.depth - 2,\n )\n }\n}\n"],"mappings":";AAkBA,SAAgB,OAAO,MAAc,KAAa,OAA0B;CAC1E,OAAO;EACL;EACA;EACA;EACA,SAAS,KAAK;EACd,YAAY,MAAM;EAClB,OAAO,KAAK,aAAa,IAAI,KAAK,MAAM,CAAC,IAAI;CAC/C;AACF;;AAGA,SAAgB,YAAY,MAAqC;CAC/D,KAAK,IAAI,QAAQ,KAAK,OAAO,QAAQ,GAAG,SAAS;EAC/C,MAAM,OAAO,KAAK,KAAK,KAAK;EAC5B,IAAI,KAAK,KAAK,SAAS,SAAS,OAAO,OAAO,MAAM,KAAK,OAAO,KAAK,GAAG,KAAK;CAC/E;CAEA,MAAM,QAAQ,KAAK;CACnB,IAAI,OAAO,KAAK,SAAS,SAAS,OAAO,OAAO,OAAO,KAAK,KAAK,KAAK,QAAQ,CAAC;CAC/E,OAAO;AACT;;AAGA,SAAgB,cAAc,KAAa,IAA8B;CACvE,IAAI,QAA0B;CAC9B,IAAI,aAAa,MAAM,QAAQ;EAC7B,IAAI,OAAO,OAAO;EAClB,IAAI,KAAK,KAAK,SAAS,WAAW,KAAK,MAAM,OAAO,IAAI;GACtD,QAAQ,OAAO,MAAM,KAAK,IAAI,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAAC;GACpD,OAAO;EACT;EAEA,OAAO,KAAK,KAAK,SAAS,WAAW,KAAK,KAAK,SAAS;CAC1D,CAAC;CACD,OAAO;AACT;AA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;CAC7E,IAAI,KAAK,OACP,OAAO,OAAO,KAAK,MAAM,YAAa,KAAK,MAAM,IAAI,KAAK,QAAQ,WAAW,GAAG,KAAK,QAAQ,CAAC;CAChG,IAAI,UAAU;CACd,SAAS;EACP,MAAM,OAAO,IAAI,QAAQ,QAAQ,GAAG;EACpC,MAAM,MAAM,QAAQ,MAAM,QAAQ,KAAK;EACvC,IAAI,KAAK,MAAM,IAAI,KAAK,OAAO,aAAa,GAC1C,OAAO,OAAO,KAAK,OAAO,MAAM,KAAK,MAAM,IAAI,CAAC,GAAG,KAAK,QAAQ,KAAK;EACvE,IAAI,QAAQ,SAAS,GAAG,OAAO;EAC/B,UAAU,OACR,KAAK,KAAK,QAAQ,QAAQ,CAAC,GAC3B,KAAK,OAAO,QAAQ,QAAQ,CAAC,GAC7B,QAAQ,QAAQ,CAClB;CACF;AACF"}
1
+ {"version":3,"file":"blocks.js","names":[],"sources":["../../src/core/blocks.ts"],"sourcesContent":["import type { Node as PMNode, ResolvedPos } from 'prosemirror-model'\nimport type { EditorState } from 'prosemirror-state'\n\n/** A `block` node and where it sits. */\nexport interface BlockInfo {\n node: PMNode\n /** The position right before the block. */\n pos: number\n /** Its depth in the document; top-level blocks are at depth 1. */\n depth: number\n /** The block's content node (paragraph, heading, …). */\n content: PMNode\n /** The position right before the content node. */\n contentPos: number\n /** The children, if any. */\n group: PMNode | null\n}\n\n/**\n * How a block lays out its children, from its content node's `layout`\n * spec field: `row` puts them side by side (a set of columns) and only\n * holds `column` blocks; a `column` only sits in a row and holds the\n * blocks shown in it. Such a block has no text of its own.\n */\nexport type BlockLayout = 'row' | 'column'\n\nexport const layoutOf = (block: PMNode | null | undefined): BlockLayout | undefined =>\n block?.firstChild?.type.spec.layout as BlockLayout | undefined\n\nexport function infoAt(node: PMNode, pos: number, depth: number): BlockInfo {\n return {\n node,\n pos,\n depth,\n content: node.firstChild!,\n contentPos: pos + 1,\n group: node.childCount > 1 ? node.child(1) : null,\n }\n}\n\n/** The innermost block around a position, or `null` between blocks. */\nexport function blockAround($pos: ResolvedPos): BlockInfo | null {\n for (let depth = $pos.depth; depth > 0; depth--) {\n const node = $pos.node(depth)\n if (node.type.name === 'block') return infoAt(node, $pos.before(depth), depth)\n }\n // right before a block, e.g. a node selection of it\n const after = $pos.nodeAfter\n if (after?.type.name === 'block') return infoAt(after, $pos.pos, $pos.depth + 1)\n return null\n}\n\n/** The block whose children `info` is one of, or `null` at the top level. */\nexport function parentBlock(doc: PMNode, info: BlockInfo): BlockInfo | null {\n if (info.depth <= 1) return null\n const $pos = doc.resolve(info.pos)\n return infoAt($pos.node(info.depth - 2), $pos.before(info.depth - 2), info.depth - 2)\n}\n\n/** The block with an id, by scanning the document. */\nexport function findBlockById(doc: PMNode, id: string): BlockInfo | null {\n let found: BlockInfo | null = null\n doc.descendants((node, pos) => {\n if (found) return false\n if (node.type.name === 'block' && node.attrs.id === id) {\n found = infoAt(node, pos, doc.resolve(pos).depth + 1)\n return false\n }\n // blocks only live in the doc and in groups; skip inline content\n return node.type.name === 'block' || node.type.name === 'blockGroup'\n })\n return found\n}\n\n/** A run of sibling blocks inside one container (the doc or a group). */\nexport interface SiblingRange {\n /** The container: the doc or a `blockGroup`. */\n parent: PMNode\n /** The position where the container's content starts. */\n parentStart: number\n /** Depth of the blocks. */\n depth: number\n /** First and last index, inclusive. */\n from: number\n to: number\n /** Positions before the first and after the last block. */\n start: number\n end: number\n}\n\nfunction rangeOf(doc: PMNode, a: BlockInfo, b: BlockInfo): SiblingRange {\n const $a = doc.resolve(a.pos)\n const $b = doc.resolve(b.pos)\n const parent = $a.parent\n return {\n parent,\n parentStart: $a.start(),\n depth: a.depth,\n from: $a.index(),\n to: $b.index(),\n start: a.pos,\n end: b.pos + b.node.nodeSize,\n }\n}\n\n/** The outer block at `depth` that contains `info`. */\nfunction climb(doc: PMNode, info: BlockInfo, depth: number): BlockInfo {\n if (info.depth === depth) return info\n const $pos = doc.resolve(info.pos + 1)\n return infoAt($pos.node(depth), $pos.before(depth), depth)\n}\n\n/**\n * The sibling blocks a selection spans. When its ends sit at different\n * levels, the range climbs to the level they share.\n */\nexport function siblingRangeBetween(doc: PMNode, from: number, to: number): SiblingRange | null {\n let a = blockAround(doc.resolve(from))\n let b = blockAround(doc.resolve(to))\n if (!a || !b) return null\n // climb two levels at a time (block > group > block) until both share a parent\n let depth = Math.min(a.depth, b.depth)\n for (;;) {\n a = climb(doc, a, depth)\n b = climb(doc, b, depth)\n if (doc.resolve(a.pos).start() === doc.resolve(b.pos).start()) break\n depth -= 2\n }\n // columns go together: a selection across them takes the whole row\n if (depth > 1) {\n const $a = doc.resolve(a.pos)\n const owner = $a.node(depth - 2)\n if (layoutOf(owner) === 'row') a = b = infoAt(owner, $a.before(depth - 2), depth - 2)\n }\n return rangeOf(doc, a, b)\n}\n\nexport function selectedRange(state: EditorState): SiblingRange | null {\n const { from, to } = state.selection\n return siblingRangeBetween(state.doc, from, to)\n}\n\n/** The blocks of a range, in order. */\nexport function blocksIn(range: SiblingRange): BlockInfo[] {\n const list: BlockInfo[] = []\n let pos = range.parentStart\n range.parent.forEach((node, offset, index) => {\n if (index >= range.from && index <= range.to) list.push(infoAt(node, pos + offset, range.depth))\n })\n return list\n}\n\n/** Every block in document order, outer before inner. */\nexport function allBlocks(doc: PMNode): BlockInfo[] {\n const list: BlockInfo[] = []\n // walked by hand: resolving every position would scan the document again each time\n const visit = (container: PMNode, start: number, depth: number) => {\n container.forEach((block, offset) => {\n const pos = start + offset\n list.push(infoAt(block, pos, depth))\n if (block.childCount > 1)\n visit(block.child(1), pos + 1 + block.firstChild!.nodeSize + 1, depth + 2)\n })\n }\n visit(doc, 0, 1)\n return list\n}\n\n/** The deepest last block inside a block (itself when it has no children). */\nexport function lastDescendant(info: BlockInfo): BlockInfo {\n let current = info\n while (current.group) {\n const group = current.group\n const lastPos =\n current.pos +\n 1 +\n current.content.nodeSize +\n 1 +\n group.content.size -\n group.lastChild!.nodeSize\n current = infoAt(group.lastChild!, lastPos, current.depth + 2)\n }\n return current\n}\n\n/** The block that comes before `info` when reading top to bottom. */\nexport function previousInDocument(doc: PMNode, info: BlockInfo): BlockInfo | null {\n const $pos = doc.resolve(info.pos)\n const index = $pos.index()\n if (index > 0) {\n const before = $pos.parent.child(index - 1)\n return lastDescendant(infoAt(before, info.pos - before.nodeSize, info.depth))\n }\n // the first child: the previous is its parent block, unless that is a\n // column, where reading starts over\n if (info.depth > 1) {\n const parent = $pos.node(info.depth - 2)\n if (layoutOf(parent) === 'column') return null\n return infoAt(parent, $pos.before(info.depth - 2), info.depth - 2)\n }\n return null\n}\n\n/** The block that comes after `info` when reading top to bottom. */\nexport function nextInDocument(doc: PMNode, info: BlockInfo): BlockInfo | null {\n if (info.group)\n return infoAt(info.group.firstChild!, info.pos + 1 + info.content.nodeSize + 1, info.depth + 2)\n let current = info\n for (;;) {\n const $pos = doc.resolve(current.pos)\n const end = current.pos + current.node.nodeSize\n if ($pos.index() < $pos.parent.childCount - 1)\n return infoAt($pos.parent.child($pos.index() + 1), end, current.depth)\n if (current.depth <= 1) return null\n const parent = $pos.node(current.depth - 2)\n // the end of a column: the next column is beside it, not after it\n if (layoutOf(parent) === 'column') return null\n current = infoAt(parent, $pos.before(current.depth - 2), current.depth - 2)\n }\n}\n"],"mappings":";AA0BA,IAAa,YAAY,UACvB,OAAO,YAAY,KAAK,KAAK;AAE/B,SAAgB,OAAO,MAAc,KAAa,OAA0B;CAC1E,OAAO;EACL;EACA;EACA;EACA,SAAS,KAAK;EACd,YAAY,MAAM;EAClB,OAAO,KAAK,aAAa,IAAI,KAAK,MAAM,CAAC,IAAI;CAC/C;AACF;;AAGA,SAAgB,YAAY,MAAqC;CAC/D,KAAK,IAAI,QAAQ,KAAK,OAAO,QAAQ,GAAG,SAAS;EAC/C,MAAM,OAAO,KAAK,KAAK,KAAK;EAC5B,IAAI,KAAK,KAAK,SAAS,SAAS,OAAO,OAAO,MAAM,KAAK,OAAO,KAAK,GAAG,KAAK;CAC/E;CAEA,MAA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AK;EACvC,IAAI,KAAK,MAAM,IAAI,KAAK,OAAO,aAAa,GAC1C,OAAO,OAAO,KAAK,OAAO,MAAM,KAAK,MAAM,IAAI,CAAC,GAAG,KAAK,QAAQ,KAAK;EACvE,IAAI,QAAQ,SAAS,GAAG,OAAO;EAC/B,MAAM,SAAS,KAAK,KAAK,QAAQ,QAAQ,CAAC;EAE1C,IAAI,SAAS,MAAM,MAAM,UAAU,OAAO;EAC1C,UAAU,OAAO,QAAQ,KAAK,OAAO,QAAQ,QAAQ,CAAC,GAAG,QAAQ,QAAQ,CAAC;CAC5E;AACF"}
@@ -1,9 +1,14 @@
1
- import { createContentNode } from "./model.js";
2
- import { blockAround, blocksIn, infoAt, nextInDocument, previousInDocument, selectedRange } from "./blocks.js";
1
+ import { createContentNode, nodeFromBlock } from "./model.js";
2
+ import { blockAround, blocksIn, findBlockById, infoAt, layoutOf, nextInDocument, parentBlock, previousInDocument, selectedRange } from "./blocks.js";
3
3
  import { NodeSelection, Selection, TextSelection } from "prosemirror-state";
4
4
  import { Fragment } from "prosemirror-model";
5
5
  // src/core/commands.ts
6
6
  var newBlock = (schema, content, children) => schema.nodes.block.create({ id: null }, children?.size ? [content, schema.nodes.blockGroup.create(null, children)] : content);
7
+ /** Whether a position is inside a column, at any depth. */
8
+ function insideColumn($pos) {
9
+ for (let depth = $pos.depth; depth > 0; depth--) if (layoutOf($pos.node(depth)) === "column") return true;
10
+ return false;
11
+ }
7
12
  /** The block and content around the cursor, when the selection is inside one textblock. */
8
13
  function cursorBlock(state) {
9
14
  const { $from, $to } = state.selection;
@@ -15,7 +20,10 @@ function cursorBlock(state) {
15
20
  * Turn every block the selection touches into `type`. Text formatting is
16
21
  * dropped when it can't be kept (in a code block); a type without text
17
22
  * (a divider, an embed) replaces the content and adds an empty paragraph
18
- * after it to keep typing in.
23
+ * after it to keep typing in. A type that starts with children of its own
24
+ * (`template` in its spec, e.g. columns) is added after the blocks instead,
25
+ * or takes the place of an empty paragraph; blocks that lay out others
26
+ * (columns) are left as they are.
19
27
  */
20
28
  function setBlockType(type, props = {}) {
21
29
  return (state, dispatch) => {
@@ -24,7 +32,13 @@ function setBlockType(type, props = {}) {
24
32
  const schema = state.schema;
25
33
  const nodeType = schema.nodes[type];
26
34
  if (!nodeType) return false;
35
+ if (nodeType.spec.layout === "row" && insideColumn(state.selection.$from)) return false;
27
36
  if (!dispatch) return true;
37
+ const template = nodeType.spec.template;
38
+ if (template) {
39
+ dispatch(insertTemplate(state.tr, blocksIn(range), type, props, template(props)));
40
+ return true;
41
+ }
28
42
  const tr = state.tr;
29
43
  const created = createContentNode(schema, type, props);
30
44
  const attrs = created.attrs;
@@ -33,6 +47,7 @@ function setBlockType(type, props = {}) {
33
47
  const last = blocks.at(-1);
34
48
  for (let index = blocks.length - 1; index >= 0; index--) {
35
49
  const block = blocks[index];
50
+ if (block.content.type.spec.layout) continue;
36
51
  tr.replaceWith(block.contentPos, block.contentPos + block.content.nodeSize, created);
37
52
  }
38
53
  const after = tr.mapping.map(last.pos + last.node.nodeSize);
@@ -44,7 +59,7 @@ function setBlockType(type, props = {}) {
44
59
  for (const block of blocksIn(range)) {
45
60
  const from = tr.mapping.map(block.contentPos);
46
61
  const content = tr.doc.nodeAt(from);
47
- if (!content.isAtom && !content.inlineContent) continue;
62
+ if (!content.isAtom && !content.inlineContent || content.type.spec.layout) continue;
48
63
  if (content.isAtom) tr.replaceWith(from, from + content.nodeSize, nodeType.create(attrs));
49
64
  else {
50
65
  const align = content.attrs.align;
@@ -62,6 +77,27 @@ function setBlockType(type, props = {}) {
62
77
  };
63
78
  }
64
79
  /**
80
+ * A new block of `type` with its starting children, after `blocks` (or in
81
+ * place of the one empty paragraph), with the cursor in its first text.
82
+ */
83
+ function insertTemplate(tr, blocks, type, props, children) {
84
+ const schema = tr.doc.type.schema;
85
+ const last = blocks.at(-1);
86
+ const node = nodeFromBlock(schema, {
87
+ type,
88
+ props,
89
+ children
90
+ });
91
+ const empty = blocks.length === 1 && !last.group && last.content.type.name === "paragraph" && last.content.content.size === 0;
92
+ const at = empty ? last.pos : last.pos + last.node.nodeSize;
93
+ if (empty) tr.replaceWith(at, at + last.node.nodeSize, node);
94
+ else tr.insert(at, node);
95
+ const $at = tr.doc.resolve(at);
96
+ if ($at.index() === $at.parent.childCount - 1) tr.insert(at + node.nodeSize, newBlock(schema, schema.nodes.paragraph.create()));
97
+ tr.setSelection(Selection.near(tr.doc.resolve(at + 1)));
98
+ return tr.scrollIntoView();
99
+ }
100
+ /**
65
101
  * Split the block at the cursor. An empty list item turns back into a
66
102
  * paragraph, an empty nested paragraph moves up a level, and a block with
67
103
  * children gets its new block as the first child.
@@ -83,7 +119,7 @@ var splitBlock = (state, dispatch) => {
83
119
  if (dispatch) dispatch(tr.setBlockType($from.pos, $from.pos, schema.nodes.paragraph).scrollIntoView());
84
120
  return true;
85
121
  }
86
- if (type === "paragraph" && info.depth > 1) return liftBlocks(state, dispatch);
122
+ if (type === "paragraph" && info.depth > 1 && liftBlocks(state)) return liftBlocks(state, dispatch);
87
123
  }
88
124
  if (!dispatch) return true;
89
125
  const offset = $from.parentOffset;
@@ -167,9 +203,18 @@ var joinBackward = (state, dispatch) => {
167
203
  }
168
204
  return true;
169
205
  }
170
- if (info.depth > 1) return liftBlocks(state, dispatch);
206
+ if (info.depth > 1 && liftBlocks(state)) return liftBlocks(state, dispatch);
171
207
  const previous = previousInDocument(state.doc, info);
172
- if (!previous) return false;
208
+ if (!previous) {
209
+ if (!insideColumn(state.selection.$from)) return false;
210
+ if (dispatch && info.content.content.size === 0 && !info.group) {
211
+ const tr = deleteBlocks(state.tr, [info]);
212
+ const column = parentBlock(state.doc, info);
213
+ if (column?.group?.childCount === 1 && state.doc.resolve(column.pos).index() > 0) tr.setSelection(Selection.near(tr.doc.resolve(tr.mapping.map(column.pos)), -1));
214
+ dispatch(tr.scrollIntoView());
215
+ }
216
+ return true;
217
+ }
173
218
  if (!dispatch) return true;
174
219
  const tr = state.tr;
175
220
  if (previous.content.isAtom) {
@@ -187,7 +232,8 @@ var joinForward = (state, dispatch) => {
187
232
  const { $from } = state.selection;
188
233
  if (!info || !state.selection.empty || $from.parentOffset < $from.parent.content.size) return false;
189
234
  const next = nextInDocument(state.doc, info);
190
- if (!next) return false;
235
+ if (!next) return insideColumn($from);
236
+ if (layoutOf(next.node)) return true;
191
237
  if (!dispatch) return true;
192
238
  const tr = state.tr;
193
239
  if (next.content.isAtom && !next.group) tr.delete(next.pos, next.pos + next.node.nodeSize);
@@ -212,6 +258,7 @@ var sinkBlocks = (state, dispatch) => {
212
258
  const schema = state.schema;
213
259
  const blocks = blocksIn(range);
214
260
  const previousNode = range.parent.child(range.from - 1);
261
+ if (layoutOf(previousNode)) return false;
215
262
  const previous = infoAt(previousNode, range.start - previousNode.nodeSize, range.depth);
216
263
  const moving = Fragment.fromArray(blocks.map((block) => block.node));
217
264
  const tr = state.tr;
@@ -239,10 +286,11 @@ var sinkBlocks = (state, dispatch) => {
239
286
  var liftBlocks = (state, dispatch) => {
240
287
  const range = selectedRange(state);
241
288
  if (!range || range.depth <= 1) return false;
242
- if (!dispatch) return true;
243
- const schema = state.schema;
244
289
  const $start = state.doc.resolve(range.start);
245
290
  const parentDepth = range.depth - 2;
291
+ if (layoutOf($start.node(parentDepth))) return false;
292
+ if (!dispatch) return true;
293
+ const schema = state.schema;
246
294
  const parent = infoAt($start.node(parentDepth), $start.before(parentDepth), parentDepth);
247
295
  const group = range.parent;
248
296
  const blocks = blocksIn(range).map((block) => block.node);
@@ -276,13 +324,17 @@ function restoreSelection(tr, state, from, to) {
276
324
  }
277
325
  tr.setSelection(TextSelection.between(tr.doc.resolve(clamp(from)), tr.doc.resolve(clamp(to))));
278
326
  }
279
- /** `Mod+Shift+↑/↓`: swap the selected blocks with their neighbour. */
327
+ /**
328
+ * `Mod+Shift+↑/↓`: swap the selected blocks with their neighbour. At the
329
+ * top or bottom of a column, the blocks move out of the set of columns,
330
+ * above or below it.
331
+ */
280
332
  function swapBlocks(direction) {
281
333
  return (state, dispatch) => {
282
334
  const range = selectedRange(state);
283
335
  if (!range) return false;
284
336
  const neighbourIndex = direction < 0 ? range.from - 1 : range.to + 1;
285
- if (neighbourIndex < 0 || neighbourIndex >= range.parent.childCount) return false;
337
+ if (neighbourIndex < 0 || neighbourIndex >= range.parent.childCount) return leaveColumn(state, range, direction, dispatch);
286
338
  if (!dispatch) return true;
287
339
  const neighbour = range.parent.child(neighbourIndex);
288
340
  const tr = state.tr;
@@ -303,6 +355,27 @@ function swapBlocks(direction) {
303
355
  return true;
304
356
  };
305
357
  }
358
+ /** The blocks of `range`, at the edge of a column, moved above or below its set of columns. */
359
+ function leaveColumn(state, range, direction, dispatch) {
360
+ if (range.depth < 5) return false;
361
+ const $start = state.doc.resolve(range.start);
362
+ const column = $start.node(range.depth - 2);
363
+ const row = $start.node(range.depth - 4);
364
+ if (layoutOf(column) !== "column" || layoutOf(row) !== "row") return false;
365
+ const rowPos = $start.before(range.depth - 4);
366
+ const target = direction < 0 ? rowPos : rowPos + row.nodeSize;
367
+ if (!dispatch) return true;
368
+ const { from, to } = state.selection;
369
+ const first = state.doc.resolve(range.start).nodeAfter.attrs.id;
370
+ let result = null;
371
+ moveRangeTo(range, target)(state, (tr) => result = tr);
372
+ if (!result) return false;
373
+ const tr = result;
374
+ const shift = findBlockById(tr.doc, first).pos - range.start;
375
+ restoreSelection(tr, state, from + shift, to + shift);
376
+ dispatch(tr.scrollIntoView());
377
+ return true;
378
+ }
306
379
  /**
307
380
  * Drag and drop: move the blocks of `range` to `target`, a position right
308
381
  * before or after a block, possibly at another level. Fails when the
@@ -324,6 +397,45 @@ function moveRangeTo(range, target) {
324
397
  return true;
325
398
  };
326
399
  }
400
+ /**
401
+ * Drag and drop beside a column: move the blocks of `range` into a new
402
+ * column of the row at `rowPos`, before its column `index` (or after the
403
+ * last). The new column is as wide as the others on average. A column the
404
+ * blocks leave empty goes away. Fails when the row is among the moved
405
+ * blocks or already has as many columns as it may (`maxColumns`).
406
+ */
407
+ function moveRangeToNewColumn(range, rowPos, index) {
408
+ return (state, dispatch) => {
409
+ const row = state.doc.nodeAt(rowPos);
410
+ const schema = state.schema;
411
+ const columnType = Object.values(schema.nodes).find((type) => type.spec.layout === "column");
412
+ if (!row || layoutOf(row) !== "row" || row.childCount < 2 || !columnType) return false;
413
+ if (rowPos >= range.start && rowPos < range.end) return false;
414
+ const group = row.child(1);
415
+ const start = rowPos + 1 + row.firstChild.nodeSize + 1;
416
+ const wholeColumn = range.depth >= 3 && range.from === 0 && range.to === range.parent.childCount - 1 && state.doc.resolve(range.start).start(range.depth - 3) === start;
417
+ const max = row.firstChild.type.spec.maxColumns ?? Infinity;
418
+ if (group.childCount >= max && !wholeColumn) return false;
419
+ if (!dispatch) return true;
420
+ let at = start;
421
+ const widths = [];
422
+ group.forEach((column, _offset, position) => {
423
+ if (position < index) at += column.nodeSize;
424
+ widths.push(Number(column.firstChild.attrs.width) || 1);
425
+ });
426
+ const average = Math.round(widths.reduce((sum, width) => sum + width, 0) / widths.length * 1e3) / 1e3;
427
+ const nodes = blocksIn(range).map((block) => block.node);
428
+ const column = schema.nodes.block.create({ id: null }, [columnType.create(average === 1 ? {} : { width: average }), schema.nodes.blockGroup.create(null, nodes)]);
429
+ const tr = state.tr;
430
+ if (range.parent.type.name === "blockGroup" && range.from === 0 && range.to === range.parent.childCount - 1) tr.delete(range.start - 1, range.end + 1);
431
+ else tr.delete(range.start, range.end);
432
+ const pos = tr.mapping.map(at);
433
+ tr.insert(pos, column);
434
+ tr.setSelection(Selection.near(tr.doc.resolve(pos + 1)));
435
+ dispatch(tr.scrollIntoView());
436
+ return true;
437
+ };
438
+ }
327
439
  /** Remove blocks, keeping at least one empty paragraph in the document. */
328
440
  function deleteBlocks(tr, blocks) {
329
441
  const sorted = [...blocks].sort((a, b) => b.pos - a.pos);
@@ -374,6 +486,6 @@ function insertBlockAfter(pos, type = "paragraph") {
374
486
  return true;
375
487
  };
376
488
  }
377
- export { deleteBlocks, deleteSelectedAtom, duplicateBlocks, exitCode, insertBlockAfter, insertBreak, joinBackward, joinForward, liftBlocks, moveRangeTo, newlineInCode, setBlockType, sinkBlocks, splitBlock, swapBlocks, withoutIds };
489
+ export { deleteBlocks, deleteSelectedAtom, duplicateBlocks, exitCode, insertBlockAfter, insertBreak, joinBackward, joinForward, liftBlocks, moveRangeTo, moveRangeToNewColumn, newlineInCode, setBlockType, sinkBlocks, splitBlock, swapBlocks, withoutIds };
378
490
 
379
491
  //# sourceMappingURL=commands.js.map