@dou-ui/flow 0.1.0-alpha.29 → 0.1.0-alpha.30

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 (44) hide show
  1. package/dist/components/flow-toolbar.js.map +1 -1
  2. package/dist/components/flow-toolbar.vue_vue_type_script_setup_true_lang.js +300 -9
  3. package/dist/components/flow-toolbar.vue_vue_type_script_setup_true_lang.js.map +1 -1
  4. package/dist/export/color.js +49 -6
  5. package/dist/export/color.js.map +1 -1
  6. package/dist/export/context.js +16 -9
  7. package/dist/export/context.js.map +1 -1
  8. package/dist/export/draw.js +6 -5
  9. package/dist/export/draw.js.map +1 -1
  10. package/dist/flow-editor.js.map +1 -1
  11. package/dist/flow-editor.vue_vue_type_script_setup_true_lang.js +318 -87
  12. package/dist/flow-editor.vue_vue_type_script_setup_true_lang.js.map +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/locale.js +90 -3
  15. package/dist/locale.js.map +1 -1
  16. package/dist/style.css +1 -1
  17. package/dist/style.js +1 -0
  18. package/dist/types/core/menu.d.ts +55 -0
  19. package/dist/types/export/color.d.ts +6 -1
  20. package/dist/types/flow-editor.vue.d.ts +1 -1
  21. package/dist/types/index.d.ts +4 -4
  22. package/dist/types/locale.d.ts +19 -1
  23. package/dist/types/whiteboard/board-node.vue.d.ts +5 -0
  24. package/dist/types/whiteboard/colors.d.ts +36 -0
  25. package/dist/types/whiteboard/index.d.ts +2 -2
  26. package/dist/types/whiteboard/model.d.ts +22 -3
  27. package/dist/types/whiteboard/toolbar.d.ts +46 -0
  28. package/dist/whiteboard/board-node.js.map +1 -1
  29. package/dist/whiteboard/board-node.vue_vue_type_script_setup_true_lang.js +50 -10
  30. package/dist/whiteboard/board-node.vue_vue_type_script_setup_true_lang.js.map +1 -1
  31. package/dist/whiteboard/colors.js +84 -0
  32. package/dist/whiteboard/colors.js.map +1 -0
  33. package/dist/whiteboard/connector.js +42 -3
  34. package/dist/whiteboard/connector.js.map +1 -1
  35. package/dist/whiteboard/draw.js +26 -9
  36. package/dist/whiteboard/draw.js.map +1 -1
  37. package/dist/whiteboard/index.js +2 -1
  38. package/dist/whiteboard/kit.js +2 -0
  39. package/dist/whiteboard/kit.js.map +1 -1
  40. package/dist/whiteboard/model.js +2 -1
  41. package/dist/whiteboard/model.js.map +1 -1
  42. package/dist/whiteboard/toolbar.js +296 -0
  43. package/dist/whiteboard/toolbar.js.map +1 -0
  44. package/package.json +3 -3
@@ -1 +1 @@
1
- {"version":3,"file":"flow-toolbar.js","names":["_sfc_main"],"sources":["../../src/components/flow-toolbar.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport { DouTooltip } from 'dou-ui'\nimport { computed, ref, shallowReactive, useTemplateRef, watch } from 'vue'\nimport { useFlowEditor } from '../context'\nimport type { FlowToolbarItem } from '../core/menu'\n\nconst props = defineProps<{\n /** Each extension's buttons, with a divider between them. */\n groups: FlowToolbarItem[][]\n /** The selection on screen, in px from the editor's top left. */\n anchor: {\n x: number\n top: number\n bottom: number\n /** Boxes it should rather not cover, e.g. nodes beside selected edges, in the same px. */\n avoid?: { left: number; top: number; right: number; bottom: number }[]\n }\n size: { width: number; height: number }\n}>()\n\nconst { messages, root, showMenu, menuFrom } = useFlowEditor()\n\nconst opensMenu = (item: FlowToolbarItem) => !!item.children?.length\n\n// A button with `children` opens its choices under it; a click from the\n// keyboard (no pointer, `detail` 0) puts focus on the one in use.\nfunction press(item: FlowToolbarItem, event: MouseEvent) {\n if (!opensMenu(item)) return item.run?.()\n const button = event.currentTarget as HTMLElement\n const box = button.getBoundingClientRect()\n showMenu(item.children!, { x: box.left, y: box.bottom + 4 }, button, {\n focusChecked: event.detail === 0,\n })\n}\n// By key: extensions give new item objects on every update.\nconst warned = new Set<string>()\n/** Says once when a button does nothing when pressed. */\nfunction warnOnce(item: FlowToolbarItem) {\n // the app's bundler replaces this, as for Vue's own warnings\n if (process.env.NODE_ENV === 'production' || warned.has(item.key)) return\n if (item.run || opensMenu(item)) return\n warned.add(item.key)\n console.warn(\n `[@dou-ui/flow] the toolbar item \"${item.key}\" has no run or children: pressing it does nothing`,\n )\n}\nwatch(\n () => props.groups,\n (groups) => groups.flat().forEach(warnOnce),\n { immediate: true },\n)\n\nconst el = useTemplateRef<HTMLElement>('el')\nconst box = ref({ width: 0, height: 0 })\n\nwatch(\n [el, () => props.groups],\n () => {\n if (el.value) box.value = { width: el.value.offsetWidth, height: el.value.offsetHeight }\n },\n { flush: 'post' },\n)\n\n/** Room between the toolbar and the selection, and the editor's edges. */\nconst GAP = 8\n\n/*\n * What it keeps clear of is read from the page, so it is read again once the\n * page has caught up with the selection: a handle drawn for the selected node\n * appears in the same update as the toolbar's new anchor.\n */\nconst measured = ref(0)\nwatch(\n () => props.anchor,\n () => measured.value++,\n { flush: 'post', immediate: true },\n)\n\n/**\n * Boxes of what the toolbar must not cover, from the editor's top left: what\n * floats over the canvas, e.g. a whiteboard's tools, and handles on the\n * selection, e.g. a whiteboard's turn handle.\n */\nfunction overlays() {\n void measured.value\n const editor = root.value\n if (!editor) return []\n const origin = editor.getBoundingClientRect()\n return [...editor.querySelectorAll<HTMLElement>('[data-flow-overlay]')].map((item) => {\n const r = item.getBoundingClientRect()\n return {\n left: r.left - origin.left,\n top: r.top - origin.top,\n right: r.right - origin.left,\n bottom: r.bottom - origin.top,\n }\n })\n}\n\n// Above the selection, below it when there is no room or it would cover\n// something floating there, kept inside the editor.\nconst place = computed(() => {\n const { width, height } = box.value\n const { anchor, size } = props\n const left = Math.min(Math.max(anchor.x - width / 2, GAP), size.width - width - GAP)\n const above = anchor.top - GAP - height\n const under = anchor.bottom + GAP\n const boxes = [...overlays(), ...(anchor.avoid ?? [])]\n const hits = (top: number) =>\n boxes.filter(\n (o) => left < o.right && left + width > o.left && top < o.bottom && top + height > o.top,\n )\n const fits = (top: number) => top >= GAP && top + height <= size.height - GAP && !hits(top).length\n // Above, else below; when something is in the way, just past it.\n const candidates = [above, under]\n const over = hits(above)\n if (over.length) candidates.push(Math.min(...over.map((o) => o.top)) - GAP - height)\n const beneath = hits(under)\n if (beneath.length) candidates.push(Math.max(...beneath.map((o) => o.bottom)) + GAP)\n const top = candidates.find(fits) ?? (above >= GAP ? above : under)\n return { left, top: Math.min(Math.max(top, GAP), size.height - height - GAP) }\n})\nconst style = computed(() => ({\n left: `${Math.round(place.value.left)}px`,\n top: `${Math.round(place.value.top)}px`,\n}))\n/** Tips open away from the selection, so they never cover it. */\nconst tipPlacement = computed(() => (place.value.top < props.anchor.top ? 'top' : 'bottom'))\n\n/** The buttons by key, to tell which one opened the menu. */\nconst buttons = shallowReactive(new Map<string, Element>())\nfunction setButton(key: string, button: unknown) {\n if (button instanceof Element) buttons.set(key, button)\n else buttons.delete(key)\n}\n\n// The selection scrolled out of view: nothing to point at.\nconst hidden = computed(\n () =>\n props.anchor.bottom < 0 ||\n props.anchor.top > props.size.height ||\n props.anchor.x < 0 ||\n props.anchor.x > props.size.width,\n)\n</script>\n\n<template>\n <div\n v-show=\"!hidden\"\n ref=\"el\"\n class=\"dou-flow-toolbar\"\n role=\"toolbar\"\n :aria-label=\"messages.toolbar\"\n :style=\"style\"\n >\n <template v-for=\"(group, i) in groups\" :key=\"i\">\n <span v-if=\"i > 0\" class=\"dou-flow-controls__divider\" />\n <!-- The tip stands aside while the button's menu is open. -->\n <DouTooltip\n v-for=\"item in group\"\n :key=\"item.key\"\n :content=\"item.label\"\n :shortcut=\"item.shortcut\"\n :disabled=\"!!menuFrom && menuFrom === buttons.get(item.key)\"\n :placement=\"tipPlacement\"\n >\n <button\n :ref=\"(button) => setButton(item.key, button)\"\n type=\"button\"\n class=\"dou-flow-controls__button\"\n :class=\"{ 'is-active': item.pressed }\"\n :disabled=\"item.disabled\"\n :aria-label=\"item.label\"\n :aria-pressed=\"opensMenu(item) ? undefined : (item.pressed ?? undefined)\"\n :aria-haspopup=\"opensMenu(item) ? 'menu' : undefined\"\n :aria-expanded=\"\n opensMenu(item) ? !!menuFrom && menuFrom === buttons.get(item.key) : undefined\n \"\n @click=\"press(item, $event)\"\n >\n <component :is=\"item.icon\" />\n </button>\n </DouTooltip>\n </template>\n </div>\n</template>\n"],"mappings":""}
1
+ {"version":3,"file":"flow-toolbar.js","names":["_sfc_main"],"sources":["../../src/components/flow-toolbar.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport { PaletteIcon } from '@dou-ui/icons'\nimport { DouColorPanel, DouPopover, DouTooltip } from 'dou-ui'\nimport {\n computed,\n nextTick,\n onBeforeUnmount,\n ref,\n shallowReactive,\n useTemplateRef,\n watch,\n} from 'vue'\nimport { useFlowEditor } from '../context'\nimport type { FlowToolbarColor, FlowToolbarItem } from '../core/menu'\nimport { plainColor } from '../export/color'\n\nconst props = defineProps<{\n /** Each extension's buttons, with a divider between them. */\n groups: FlowToolbarItem[][]\n /** The selection on screen, in px from the editor's top left. */\n anchor: {\n x: number\n top: number\n bottom: number\n /** Boxes it should rather not cover, e.g. nodes beside selected edges, in the same px. */\n avoid?: { left: number; top: number; right: number; bottom: number }[]\n }\n size: { width: number; height: number }\n}>()\n\nconst { messages, root, selection, showMenu, menuFrom } = useFlowEditor()\n\nconst opensMenu = (item: FlowToolbarItem) => !!item.children?.length\nconst opensColor = (item: FlowToolbarItem) => !!item.color\n\n// Color popover state -------------------------------------------------------------------\n\nconst openColor = ref<string | null>(null)\nconst customKey = ref<string | null>(null)\nconst customColor = ref<string | null>(null)\nconst customPlacement = ref<'right-start' | 'left-start' | 'bottom-end' | 'top-end'>('right-start')\nconst customOffset = ref(19)\nconst swatchesEls = new Map<string, HTMLElement>()\nlet closeActiveGroup: (() => void) | null = null\n\nfunction setSwatchesEl(key: string, el: unknown) {\n if (el instanceof HTMLElement) swatchesEls.set(key, el)\n else swatchesEls.delete(key)\n}\n\nfunction finishColorGroup() {\n closeActiveGroup?.()\n closeActiveGroup = null\n}\n\nfunction closeColorPopover() {\n finishColorGroup()\n openColor.value = null\n customKey.value = null\n}\n\nfunction isCustomColor(color: FlowToolbarColor) {\n const value = color.value\n if (!value || color.none || !color.swatch) return false\n return !color.presets.some((preset) => preset.value === value)\n}\n\nfunction customSwatch(color: FlowToolbarColor) {\n return isCustomColor(color) ? color.swatch : undefined\n}\n\nfunction setColorOpen(key: string, open: boolean) {\n if (open) {\n if (openColor.value && openColor.value !== key) {\n finishColorGroup()\n customKey.value = null\n }\n openColor.value = key\n } else if (openColor.value === key) {\n closeColorPopover()\n }\n}\n\nfunction pickColor(color: FlowToolbarColor, value: string | undefined) {\n finishColorGroup()\n color.select(value, { live: false })\n openColor.value = null\n customKey.value = null\n void nextTick(() => root.value?.focus({ preventScroll: true }))\n}\n\nconst CSS_COLOR = /^(#|rgb|hsl|hsv|hwb|lab|lch|oklab|oklch)/i\nconst CUSTOM_PANEL_WIDTH = 258\nconst CUSTOM_PANEL_HEIGHT = 260\nconst CUSTOM_POPOVER_GAP = 8\nconst VIEWPORT_PADDING = 8\n\n/** Place the secondary color picker cleanly beside (or above/below) the primary swatch popover without overlapping it. */\nfunction placeCustomPopover(swatches: HTMLElement | undefined) {\n const primaryPanel = swatches?.closest<HTMLElement>('.dou-flow-color-popover')\n const customButton = swatches?.querySelector<HTMLElement>('.is-custom')\n if (!primaryPanel || !customButton) {\n customPlacement.value = 'right-start'\n customOffset.value = 19\n return\n }\n const panelRect = primaryPanel.getBoundingClientRect()\n const btnRect = customButton.getBoundingClientRect()\n const vw = document.documentElement.clientWidth\n const vh = document.documentElement.clientHeight\n const roomRight = vw - panelRect.right - VIEWPORT_PADDING\n const roomLeft = panelRect.left - VIEWPORT_PADDING\n if (roomRight >= CUSTOM_PANEL_WIDTH + CUSTOM_POPOVER_GAP) {\n customPlacement.value = 'right-start'\n customOffset.value = Math.round(panelRect.right - btnRect.right + CUSTOM_POPOVER_GAP)\n } else if (roomLeft >= CUSTOM_PANEL_WIDTH + CUSTOM_POPOVER_GAP) {\n customPlacement.value = 'left-start'\n customOffset.value = Math.round(btnRect.left - panelRect.left + CUSTOM_POPOVER_GAP)\n } else {\n const roomBelow = vh - panelRect.bottom - VIEWPORT_PADDING\n const roomAbove = panelRect.top - VIEWPORT_PADDING\n if (roomBelow < CUSTOM_PANEL_HEIGHT + CUSTOM_POPOVER_GAP && roomAbove > roomBelow) {\n customPlacement.value = 'top-end'\n customOffset.value = Math.round(btnRect.top - panelRect.top + CUSTOM_POPOVER_GAP)\n } else {\n customPlacement.value = 'bottom-end'\n customOffset.value = Math.round(panelRect.bottom - btnRect.bottom + CUSTOM_POPOVER_GAP)\n }\n }\n}\n\nfunction setCustomOpen(item: FlowToolbarItem, open: boolean) {\n const color = item.color\n if (!color) return\n if (!open) {\n if (customKey.value === item.key) {\n finishColorGroup()\n customKey.value = null\n }\n return\n }\n if (customKey.value && customKey.value !== item.key) finishColorGroup()\n const swatches = swatchesEls.get(item.key)\n placeCustomPopover(swatches)\n customKey.value = item.key\n if (isCustomColor(color) && color.value && CSS_COLOR.test(color.value)) {\n customColor.value = color.value\n return\n }\n // Start from the active swatch's computed color so tweaking a preset or tone begins near it.\n const active = swatches?.querySelector<HTMLElement>('[aria-pressed=\"true\"]:not(.is-default)')\n const raw = active && getComputedStyle(active).color\n customColor.value = raw ? plainColor(raw) || null : null\n}\n\nfunction onCustomInput(color: FlowToolbarColor, value: string | null | undefined) {\n customColor.value = value ?? null\n if (value) {\n closeActiveGroup = color.close ?? null\n color.select(value, { live: true })\n }\n}\n\nfunction onCustomChange(color: FlowToolbarColor, value: string | null | undefined) {\n customColor.value = value ?? null\n if (value) color.select(value, { live: false })\n closeActiveGroup = null\n}\n\n// Close open color popover when a context menu opens, selection changes, or the button vanishes.\nwatch(menuFrom, (el) => {\n if (el) closeColorPopover()\n})\nwatch(\n () => [selection.value.nodes.join(','), selection.value.edges.join(',')],\n () => closeColorPopover(),\n)\nwatch(\n () => props.groups,\n (groups) => {\n if (openColor.value && !groups.flat().some((item) => item.key === openColor.value)) {\n closeColorPopover()\n }\n },\n)\nonBeforeUnmount(() => finishColorGroup())\n\n// A button with `children` opens its choices under it; a click from the\n// keyboard (no pointer, `detail` 0) puts focus on the one in use.\nfunction press(item: FlowToolbarItem, event: MouseEvent) {\n closeColorPopover()\n if (!opensMenu(item)) return item.run?.()\n const button = event.currentTarget as HTMLElement\n const box = button.getBoundingClientRect()\n showMenu(item.children!, { x: box.left, y: box.bottom + 4 }, button, {\n focusChecked: event.detail === 0,\n })\n}\n// By key: extensions give new item objects on every update.\nconst warned = new Set<string>()\n/** Says once when a button does nothing when pressed. */\nfunction warnOnce(item: FlowToolbarItem) {\n // the app's bundler replaces this, as for Vue's own warnings\n if (process.env.NODE_ENV === 'production' || warned.has(item.key)) return\n if (item.run || opensMenu(item) || opensColor(item)) return\n warned.add(item.key)\n console.warn(\n `[@dou-ui/flow] the toolbar item \"${item.key}\" has no run, children or color: pressing it does nothing`,\n )\n}\nwatch(\n () => props.groups,\n (groups) => groups.flat().forEach(warnOnce),\n { immediate: true },\n)\n\nconst el = useTemplateRef<HTMLElement>('el')\nconst box = ref({ width: 0, height: 0 })\n\nwatch(\n [el, () => props.groups],\n () => {\n if (el.value) box.value = { width: el.value.offsetWidth, height: el.value.offsetHeight }\n },\n { flush: 'post' },\n)\n\n/** Room between the toolbar and the selection, and the editor's edges. */\nconst GAP = 8\n\n/*\n * What it keeps clear of is read from the page, so it is read again once the\n * page has caught up with the selection: a handle drawn for the selected node\n * appears in the same update as the toolbar's new anchor.\n */\nconst measured = ref(0)\nwatch(\n () => props.anchor,\n () => measured.value++,\n { flush: 'post', immediate: true },\n)\n\n/**\n * Boxes of what the toolbar must not cover, from the editor's top left: what\n * floats over the canvas, e.g. a whiteboard's tools, and handles on the\n * selection, e.g. a whiteboard's turn handle.\n */\nfunction overlays() {\n void measured.value\n const editor = root.value\n if (!editor) return []\n const origin = editor.getBoundingClientRect()\n return [...editor.querySelectorAll<HTMLElement>('[data-flow-overlay]')].map((item) => {\n const r = item.getBoundingClientRect()\n return {\n left: r.left - origin.left,\n top: r.top - origin.top,\n right: r.right - origin.left,\n bottom: r.bottom - origin.top,\n }\n })\n}\n\n// Above the selection, below it when there is no room or it would cover\n// something floating there, kept inside the editor.\nconst place = computed(() => {\n const { width, height } = box.value\n const { anchor, size } = props\n const left = Math.min(Math.max(anchor.x - width / 2, GAP), size.width - width - GAP)\n const above = anchor.top - GAP - height\n const under = anchor.bottom + GAP\n const boxes = [...overlays(), ...(anchor.avoid ?? [])]\n const hits = (top: number) =>\n boxes.filter(\n (o) => left < o.right && left + width > o.left && top < o.bottom && top + height > o.top,\n )\n const fits = (top: number) => top >= GAP && top + height <= size.height - GAP && !hits(top).length\n // Above, else below; when something is in the way, just past it.\n const candidates = [above, under]\n const over = hits(above)\n if (over.length) candidates.push(Math.min(...over.map((o) => o.top)) - GAP - height)\n const beneath = hits(under)\n if (beneath.length) candidates.push(Math.max(...beneath.map((o) => o.bottom)) + GAP)\n const top = candidates.find(fits) ?? (above >= GAP ? above : under)\n return { left, top: Math.min(Math.max(top, GAP), size.height - height - GAP) }\n})\nconst style = computed(() => ({\n left: `${Math.round(place.value.left)}px`,\n top: `${Math.round(place.value.top)}px`,\n}))\n/** Tips open away from the selection, so they never cover it. */\nconst tipPlacement = computed(() => (place.value.top < props.anchor.top ? 'top' : 'bottom'))\nconst popoverPlacement = computed(() =>\n place.value.top < props.anchor.top ? 'top-start' : 'bottom-start',\n)\n\n/** The buttons by key, to tell which one opened the menu. */\nconst buttons = shallowReactive(new Map<string, Element>())\nfunction setButton(key: string, button: unknown) {\n if (button instanceof Element) buttons.set(key, button)\n else buttons.delete(key)\n}\n\n// The selection scrolled out of view: nothing to point at.\nconst hidden = computed(\n () =>\n props.anchor.bottom < 0 ||\n props.anchor.top > props.size.height ||\n props.anchor.x < 0 ||\n props.anchor.x > props.size.width,\n)\nwatch(hidden, (isHidden) => {\n if (isHidden) closeColorPopover()\n})\n</script>\n\n<template>\n <div\n v-show=\"!hidden\"\n ref=\"el\"\n class=\"dou-flow-toolbar\"\n role=\"toolbar\"\n :aria-label=\"messages.toolbar\"\n :style=\"style\"\n >\n <template v-for=\"(group, i) in groups\" :key=\"i\">\n <span v-if=\"i > 0\" class=\"dou-flow-controls__divider\" />\n <template v-for=\"item in group\" :key=\"item.key\">\n <DouTooltip\n v-if=\"item.color\"\n :content=\"item.label\"\n :shortcut=\"item.shortcut\"\n :disabled=\"openColor === item.key\"\n :placement=\"tipPlacement\"\n >\n <DouPopover\n :open=\"openColor === item.key\"\n :placement=\"popoverPlacement\"\n :disabled=\"item.disabled\"\n class=\"dou-flow-color-popover\"\n @update:open=\"setColorOpen(item.key, $event)\"\n >\n <button\n :ref=\"(button) => setButton(item.key, button)\"\n type=\"button\"\n class=\"dou-flow-controls__button dou-flow-toolbar__color\"\n :class=\"[\n `is-kind-${item.color.kind ?? 'stroke'}`,\n {\n 'is-active': openColor === item.key,\n 'has-swatch': !!item.color.swatch,\n 'is-none': !!item.color.none,\n },\n ]\"\n :style=\"item.color.swatch ? { '--dou-flow-swatch': item.color.swatch } : undefined\"\n :disabled=\"item.disabled\"\n :aria-label=\"item.label\"\n aria-haspopup=\"dialog\"\n :aria-expanded=\"openColor === item.key\"\n >\n <span class=\"dou-flow-toolbar__color-glyph\">\n <component :is=\"item.icon\" />\n <span class=\"dou-flow-toolbar__color-bar\" aria-hidden=\"true\">\n <template v-if=\"item.color.none\"> <span /><span /><span /> </template>\n </span>\n </span>\n </button>\n <template #content>\n <div class=\"dou-flow-color-popover__body\">\n <div\n :ref=\"(el) => setSwatchesEl(item.key, el)\"\n class=\"dou-flow-color-popover__swatches\"\n role=\"group\"\n :aria-label=\"item.label\"\n >\n <button\n type=\"button\"\n class=\"dou-flow-color-popover__swatch is-default\"\n :class=\"`is-kind-${item.color.kind ?? 'stroke'}`\"\n :aria-pressed=\"item.color.value === undefined\"\n :aria-label=\"item.color.defaultLabel\"\n :title=\"item.color.defaultLabel\"\n @click=\"pickColor(item.color, undefined)\"\n />\n <button\n v-for=\"preset in item.color.presets\"\n :key=\"preset.value\"\n type=\"button\"\n class=\"dou-flow-color-popover__swatch\"\n :class=\"`is-kind-${item.color.kind ?? 'stroke'}`\"\n :style=\"{ '--dou-flow-swatch': preset.swatch }\"\n :aria-pressed=\"item.color.value === preset.value\"\n :aria-label=\"preset.label\"\n :title=\"preset.label\"\n @click=\"pickColor(item.color, preset.value)\"\n >\n <span v-if=\"item.color.kind === 'text'\" aria-hidden=\"true\">A</span>\n </button>\n <DouPopover\n :open=\"customKey === item.key\"\n :placement=\"customPlacement\"\n :offset=\"customOffset\"\n class=\"dou-flow-color-custom-popover\"\n @update:open=\"setCustomOpen(item, $event)\"\n >\n <button\n type=\"button\"\n class=\"dou-flow-color-popover__swatch is-custom\"\n :class=\"[\n `is-kind-${item.color.kind ?? 'stroke'}`,\n { 'has-custom': isCustomColor(item.color) },\n ]\"\n :style=\"\n customSwatch(item.color)\n ? { '--dou-flow-swatch': customSwatch(item.color) }\n : undefined\n \"\n :aria-pressed=\"isCustomColor(item.color)\"\n :aria-label=\"item.color.customLabel\"\n :title=\"item.color.customLabel\"\n >\n <span\n v-if=\"isCustomColor(item.color) && item.color.kind === 'text'\"\n aria-hidden=\"true\"\n >\n A\n </span>\n <PaletteIcon v-else-if=\"!isCustomColor(item.color)\" />\n </button>\n <template #content>\n <DouColorPanel\n :model-value=\"customKey === item.key ? customColor : null\"\n :bordered=\"false\"\n format=\"hex\"\n show-alpha\n show-input\n @update:model-value=\"onCustomInput(item.color, $event)\"\n @change=\"onCustomChange(item.color, $event)\"\n />\n </template>\n </DouPopover>\n </div>\n <button\n v-if=\"item.color.noneOption\"\n type=\"button\"\n class=\"dou-flow-color-popover__none\"\n :aria-pressed=\"!!item.color.none\"\n @click=\"pickColor(item.color, item.color.noneOption.value)\"\n >\n <component\n :is=\"item.color.noneOption.icon\"\n v-if=\"item.color.noneOption.icon\"\n class=\"dou-flow-color-popover__none-icon\"\n />\n <span v-else class=\"dou-flow-color-popover__none-swatch\" aria-hidden=\"true\" />\n <span>{{ item.color.noneOption.label }}</span>\n </button>\n </div>\n </template>\n </DouPopover>\n </DouTooltip>\n <!-- The tip stands aside while the button's menu is open. -->\n <DouTooltip\n v-else\n :content=\"item.label\"\n :shortcut=\"item.shortcut\"\n :disabled=\"!!menuFrom && menuFrom === buttons.get(item.key)\"\n :placement=\"tipPlacement\"\n >\n <button\n :ref=\"(button) => setButton(item.key, button)\"\n type=\"button\"\n class=\"dou-flow-controls__button\"\n :class=\"{ 'is-active': item.pressed }\"\n :disabled=\"item.disabled\"\n :aria-label=\"item.label\"\n :aria-pressed=\"opensMenu(item) ? undefined : (item.pressed ?? undefined)\"\n :aria-haspopup=\"opensMenu(item) ? 'menu' : undefined\"\n :aria-expanded=\"\n opensMenu(item) ? !!menuFrom && menuFrom === buttons.get(item.key) : undefined\n \"\n @click=\"press(item, $event)\"\n >\n <component :is=\"item.icon\" />\n </button>\n </DouTooltip>\n </template>\n </template>\n </div>\n</template>\n"],"mappings":""}
@@ -1,6 +1,8 @@
1
1
  import { useFlowEditor } from "../context.js";
2
- import { DouTooltip } from "dou-ui";
3
- import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, defineComponent, normalizeClass, normalizeStyle, openBlock, ref, renderList, resolveDynamicComponent, shallowReactive, unref, useTemplateRef, vShow, watch, withCtx, withDirectives } from "vue";
2
+ import { plainColor } from "../export/color.js";
3
+ import { DouColorPanel, DouPopover, DouTooltip } from "dou-ui";
4
+ import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createVNode, defineComponent, nextTick, normalizeClass, normalizeStyle, onBeforeUnmount, openBlock, ref, renderList, resolveDynamicComponent, shallowReactive, toDisplayString, unref, useTemplateRef, vShow, watch, withCtx, withDirectives } from "vue";
5
+ import { PaletteIcon } from "@dou-ui/icons";
4
6
  // src/components/flow-toolbar.vue?vue&type=script&setup=true&lang.ts
5
7
  var _hoisted_1 = ["aria-label"];
6
8
  var _hoisted_2 = {
@@ -8,6 +10,49 @@ var _hoisted_2 = {
8
10
  class: "dou-flow-controls__divider"
9
11
  };
10
12
  var _hoisted_3 = [
13
+ "disabled",
14
+ "aria-label",
15
+ "aria-expanded"
16
+ ];
17
+ var _hoisted_4 = { class: "dou-flow-toolbar__color-glyph" };
18
+ var _hoisted_5 = {
19
+ class: "dou-flow-toolbar__color-bar",
20
+ "aria-hidden": "true"
21
+ };
22
+ var _hoisted_6 = { class: "dou-flow-color-popover__body" };
23
+ var _hoisted_7 = ["aria-label"];
24
+ var _hoisted_8 = [
25
+ "aria-pressed",
26
+ "aria-label",
27
+ "title",
28
+ "onClick"
29
+ ];
30
+ var _hoisted_9 = [
31
+ "aria-pressed",
32
+ "aria-label",
33
+ "title",
34
+ "onClick"
35
+ ];
36
+ var _hoisted_10 = {
37
+ key: 0,
38
+ "aria-hidden": "true"
39
+ };
40
+ var _hoisted_11 = [
41
+ "aria-pressed",
42
+ "aria-label",
43
+ "title"
44
+ ];
45
+ var _hoisted_12 = {
46
+ key: 0,
47
+ "aria-hidden": "true"
48
+ };
49
+ var _hoisted_13 = ["aria-pressed", "onClick"];
50
+ var _hoisted_14 = {
51
+ key: 1,
52
+ class: "dou-flow-color-popover__none-swatch",
53
+ "aria-hidden": "true"
54
+ };
55
+ var _hoisted_15 = [
11
56
  "disabled",
12
57
  "aria-label",
13
58
  "aria-pressed",
@@ -15,6 +60,9 @@ var _hoisted_3 = [
15
60
  "aria-expanded",
16
61
  "onClick"
17
62
  ];
63
+ var CUSTOM_POPOVER_GAP = 8;
64
+ var VIEWPORT_PADDING = 8;
65
+ /** Place the secondary color picker cleanly beside (or above/below) the primary swatch popover without overlapping it. */
18
66
  var GAP = 8;
19
67
  var flow_toolbar_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defineComponent({
20
68
  __name: "flow-toolbar",
@@ -25,9 +73,130 @@ var flow_toolbar_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ def
25
73
  },
26
74
  setup(__props) {
27
75
  const props = __props;
28
- const { messages, root, showMenu, menuFrom } = useFlowEditor();
76
+ const { messages, root, selection, showMenu, menuFrom } = useFlowEditor();
29
77
  const opensMenu = (item) => !!item.children?.length;
78
+ const opensColor = (item) => !!item.color;
79
+ const openColor = ref(null);
80
+ const customKey = ref(null);
81
+ const customColor = ref(null);
82
+ const customPlacement = ref("right-start");
83
+ const customOffset = ref(19);
84
+ const swatchesEls = /* @__PURE__ */ new Map();
85
+ let closeActiveGroup = null;
86
+ function setSwatchesEl(key, el) {
87
+ if (el instanceof HTMLElement) swatchesEls.set(key, el);
88
+ else swatchesEls.delete(key);
89
+ }
90
+ function finishColorGroup() {
91
+ closeActiveGroup?.();
92
+ closeActiveGroup = null;
93
+ }
94
+ function closeColorPopover() {
95
+ finishColorGroup();
96
+ openColor.value = null;
97
+ customKey.value = null;
98
+ }
99
+ function isCustomColor(color) {
100
+ const value = color.value;
101
+ if (!value || color.none || !color.swatch) return false;
102
+ return !color.presets.some((preset) => preset.value === value);
103
+ }
104
+ function customSwatch(color) {
105
+ return isCustomColor(color) ? color.swatch : void 0;
106
+ }
107
+ function setColorOpen(key, open) {
108
+ if (open) {
109
+ if (openColor.value && openColor.value !== key) {
110
+ finishColorGroup();
111
+ customKey.value = null;
112
+ }
113
+ openColor.value = key;
114
+ } else if (openColor.value === key) closeColorPopover();
115
+ }
116
+ function pickColor(color, value) {
117
+ finishColorGroup();
118
+ color.select(value, { live: false });
119
+ openColor.value = null;
120
+ customKey.value = null;
121
+ nextTick(() => root.value?.focus({ preventScroll: true }));
122
+ }
123
+ const CSS_COLOR = /^(#|rgb|hsl|hsv|hwb|lab|lch|oklab|oklch)/i;
124
+ function placeCustomPopover(swatches) {
125
+ const primaryPanel = swatches?.closest(".dou-flow-color-popover");
126
+ const customButton = swatches?.querySelector(".is-custom");
127
+ if (!primaryPanel || !customButton) {
128
+ customPlacement.value = "right-start";
129
+ customOffset.value = 19;
130
+ return;
131
+ }
132
+ const panelRect = primaryPanel.getBoundingClientRect();
133
+ const btnRect = customButton.getBoundingClientRect();
134
+ const vw = document.documentElement.clientWidth;
135
+ const vh = document.documentElement.clientHeight;
136
+ const roomRight = vw - panelRect.right - VIEWPORT_PADDING;
137
+ const roomLeft = panelRect.left - VIEWPORT_PADDING;
138
+ if (roomRight >= 266) {
139
+ customPlacement.value = "right-start";
140
+ customOffset.value = Math.round(panelRect.right - btnRect.right + CUSTOM_POPOVER_GAP);
141
+ } else if (roomLeft >= 266) {
142
+ customPlacement.value = "left-start";
143
+ customOffset.value = Math.round(btnRect.left - panelRect.left + CUSTOM_POPOVER_GAP);
144
+ } else {
145
+ const roomBelow = vh - panelRect.bottom - VIEWPORT_PADDING;
146
+ const roomAbove = panelRect.top - VIEWPORT_PADDING;
147
+ if (roomBelow < 268 && roomAbove > roomBelow) {
148
+ customPlacement.value = "top-end";
149
+ customOffset.value = Math.round(btnRect.top - panelRect.top + CUSTOM_POPOVER_GAP);
150
+ } else {
151
+ customPlacement.value = "bottom-end";
152
+ customOffset.value = Math.round(panelRect.bottom - btnRect.bottom + CUSTOM_POPOVER_GAP);
153
+ }
154
+ }
155
+ }
156
+ function setCustomOpen(item, open) {
157
+ const color = item.color;
158
+ if (!color) return;
159
+ if (!open) {
160
+ if (customKey.value === item.key) {
161
+ finishColorGroup();
162
+ customKey.value = null;
163
+ }
164
+ return;
165
+ }
166
+ if (customKey.value && customKey.value !== item.key) finishColorGroup();
167
+ const swatches = swatchesEls.get(item.key);
168
+ placeCustomPopover(swatches);
169
+ customKey.value = item.key;
170
+ if (isCustomColor(color) && color.value && CSS_COLOR.test(color.value)) {
171
+ customColor.value = color.value;
172
+ return;
173
+ }
174
+ const active = swatches?.querySelector("[aria-pressed=\"true\"]:not(.is-default)");
175
+ const raw = active && getComputedStyle(active).color;
176
+ customColor.value = raw ? plainColor(raw) || null : null;
177
+ }
178
+ function onCustomInput(color, value) {
179
+ customColor.value = value ?? null;
180
+ if (value) {
181
+ closeActiveGroup = color.close ?? null;
182
+ color.select(value, { live: true });
183
+ }
184
+ }
185
+ function onCustomChange(color, value) {
186
+ customColor.value = value ?? null;
187
+ if (value) color.select(value, { live: false });
188
+ closeActiveGroup = null;
189
+ }
190
+ watch(menuFrom, (el) => {
191
+ if (el) closeColorPopover();
192
+ });
193
+ watch(() => [selection.value.nodes.join(","), selection.value.edges.join(",")], () => closeColorPopover());
194
+ watch(() => props.groups, (groups) => {
195
+ if (openColor.value && !groups.flat().some((item) => item.key === openColor.value)) closeColorPopover();
196
+ });
197
+ onBeforeUnmount(() => finishColorGroup());
30
198
  function press(item, event) {
199
+ closeColorPopover();
31
200
  if (!opensMenu(item)) return item.run?.();
32
201
  const button = event.currentTarget;
33
202
  const box = button.getBoundingClientRect();
@@ -40,9 +209,9 @@ var flow_toolbar_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ def
40
209
  /** Says once when a button does nothing when pressed. */
41
210
  function warnOnce(item) {
42
211
  if (process.env.NODE_ENV === "production" || warned.has(item.key)) return;
43
- if (item.run || opensMenu(item)) return;
212
+ if (item.run || opensMenu(item) || opensColor(item)) return;
44
213
  warned.add(item.key);
45
- console.warn(`[@dou-ui/flow] the toolbar item "${item.key}" has no run or children: pressing it does nothing`);
214
+ console.warn(`[@dou-ui/flow] the toolbar item "${item.key}" has no run, children or color: pressing it does nothing`);
46
215
  }
47
216
  watch(() => props.groups, (groups) => groups.flat().forEach(warnOnce), { immediate: true });
48
217
  const el = useTemplateRef("el");
@@ -108,6 +277,7 @@ var flow_toolbar_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ def
108
277
  }));
109
278
  /** Tips open away from the selection, so they never cover it. */
110
279
  const tipPlacement = computed(() => place.value.top < props.anchor.top ? "top" : "bottom");
280
+ const popoverPlacement = computed(() => place.value.top < props.anchor.top ? "top-start" : "bottom-start");
111
281
  /** The buttons by key, to tell which one opened the menu. */
112
282
  const buttons = shallowReactive(/* @__PURE__ */ new Map());
113
283
  function setButton(key, button) {
@@ -115,6 +285,9 @@ var flow_toolbar_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ def
115
285
  else buttons.delete(key);
116
286
  }
117
287
  const hidden = computed(() => props.anchor.bottom < 0 || props.anchor.top > props.size.height || props.anchor.x < 0 || props.anchor.x > props.size.width);
288
+ watch(hidden, (isHidden) => {
289
+ if (isHidden) closeColorPopover();
290
+ });
118
291
  return (_ctx, _cache) => {
119
292
  return withDirectives((openBlock(), createElementBlock("div", {
120
293
  ref_key: "el",
@@ -125,8 +298,126 @@ var flow_toolbar_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ def
125
298
  style: normalizeStyle(style.value)
126
299
  }, [(openBlock(true), createElementBlock(Fragment, null, renderList(__props.groups, (group, i) => {
127
300
  return openBlock(), createElementBlock(Fragment, { key: i }, [i > 0 ? (openBlock(), createElementBlock("span", _hoisted_2)) : createCommentVNode("", true), (openBlock(true), createElementBlock(Fragment, null, renderList(group, (item) => {
128
- return openBlock(), createBlock(unref(DouTooltip), {
129
- key: item.key,
301
+ return openBlock(), createElementBlock(Fragment, { key: item.key }, [item.color ? (openBlock(), createBlock(unref(DouTooltip), {
302
+ key: 0,
303
+ content: item.label,
304
+ shortcut: item.shortcut,
305
+ disabled: openColor.value === item.key,
306
+ placement: tipPlacement.value
307
+ }, {
308
+ default: withCtx(() => [createVNode(unref(DouPopover), {
309
+ open: openColor.value === item.key,
310
+ placement: popoverPlacement.value,
311
+ disabled: item.disabled,
312
+ class: "dou-flow-color-popover",
313
+ "onUpdate:open": ($event) => setColorOpen(item.key, $event)
314
+ }, {
315
+ content: withCtx(() => [createElementVNode("div", _hoisted_6, [createElementVNode("div", {
316
+ ref_for: true,
317
+ ref: (el) => setSwatchesEl(item.key, el),
318
+ class: "dou-flow-color-popover__swatches",
319
+ role: "group",
320
+ "aria-label": item.label
321
+ }, [
322
+ createElementVNode("button", {
323
+ type: "button",
324
+ class: normalizeClass(["dou-flow-color-popover__swatch is-default", `is-kind-${item.color.kind ?? "stroke"}`]),
325
+ "aria-pressed": item.color.value === void 0,
326
+ "aria-label": item.color.defaultLabel,
327
+ title: item.color.defaultLabel,
328
+ onClick: ($event) => pickColor(item.color, void 0)
329
+ }, null, 10, _hoisted_8),
330
+ (openBlock(true), createElementBlock(Fragment, null, renderList(item.color.presets, (preset) => {
331
+ return openBlock(), createElementBlock("button", {
332
+ key: preset.value,
333
+ type: "button",
334
+ class: normalizeClass(["dou-flow-color-popover__swatch", `is-kind-${item.color.kind ?? "stroke"}`]),
335
+ style: normalizeStyle({ "--dou-flow-swatch": preset.swatch }),
336
+ "aria-pressed": item.color.value === preset.value,
337
+ "aria-label": preset.label,
338
+ title: preset.label,
339
+ onClick: ($event) => pickColor(item.color, preset.value)
340
+ }, [item.color.kind === "text" ? (openBlock(), createElementBlock("span", _hoisted_10, "A")) : createCommentVNode("", true)], 14, _hoisted_9);
341
+ }), 128)),
342
+ createVNode(unref(DouPopover), {
343
+ open: customKey.value === item.key,
344
+ placement: customPlacement.value,
345
+ offset: customOffset.value,
346
+ class: "dou-flow-color-custom-popover",
347
+ "onUpdate:open": ($event) => setCustomOpen(item, $event)
348
+ }, {
349
+ content: withCtx(() => [createVNode(unref(DouColorPanel), {
350
+ "model-value": customKey.value === item.key ? customColor.value : null,
351
+ bordered: false,
352
+ format: "hex",
353
+ "show-alpha": "",
354
+ "show-input": "",
355
+ "onUpdate:modelValue": ($event) => onCustomInput(item.color, $event),
356
+ onChange: ($event) => onCustomChange(item.color, $event)
357
+ }, null, 8, [
358
+ "model-value",
359
+ "onUpdate:modelValue",
360
+ "onChange"
361
+ ])]),
362
+ default: withCtx(() => [createElementVNode("button", {
363
+ type: "button",
364
+ class: normalizeClass(["dou-flow-color-popover__swatch is-custom", [`is-kind-${item.color.kind ?? "stroke"}`, { "has-custom": isCustomColor(item.color) }]]),
365
+ style: normalizeStyle(customSwatch(item.color) ? { "--dou-flow-swatch": customSwatch(item.color) } : void 0),
366
+ "aria-pressed": isCustomColor(item.color),
367
+ "aria-label": item.color.customLabel,
368
+ title: item.color.customLabel
369
+ }, [isCustomColor(item.color) && item.color.kind === "text" ? (openBlock(), createElementBlock("span", _hoisted_12, " A ")) : !isCustomColor(item.color) ? (openBlock(), createBlock(unref(PaletteIcon), { key: 1 })) : createCommentVNode("", true)], 14, _hoisted_11)]),
370
+ _: 2
371
+ }, 1032, [
372
+ "open",
373
+ "placement",
374
+ "offset",
375
+ "onUpdate:open"
376
+ ])
377
+ ], 8, _hoisted_7), item.color.noneOption ? (openBlock(), createElementBlock("button", {
378
+ key: 0,
379
+ type: "button",
380
+ class: "dou-flow-color-popover__none",
381
+ "aria-pressed": !!item.color.none,
382
+ onClick: ($event) => pickColor(item.color, item.color.noneOption.value)
383
+ }, [item.color.noneOption.icon ? (openBlock(), createBlock(resolveDynamicComponent(item.color.noneOption.icon), {
384
+ key: 0,
385
+ class: "dou-flow-color-popover__none-icon"
386
+ })) : (openBlock(), createElementBlock("span", _hoisted_14)), createElementVNode("span", null, toDisplayString(item.color.noneOption.label), 1)], 8, _hoisted_13)) : createCommentVNode("", true)])]),
387
+ default: withCtx(() => [createElementVNode("button", {
388
+ ref_for: true,
389
+ ref: (button) => setButton(item.key, button),
390
+ type: "button",
391
+ class: normalizeClass(["dou-flow-controls__button dou-flow-toolbar__color", [`is-kind-${item.color.kind ?? "stroke"}`, {
392
+ "is-active": openColor.value === item.key,
393
+ "has-swatch": !!item.color.swatch,
394
+ "is-none": !!item.color.none
395
+ }]]),
396
+ style: normalizeStyle(item.color.swatch ? { "--dou-flow-swatch": item.color.swatch } : void 0),
397
+ disabled: item.disabled,
398
+ "aria-label": item.label,
399
+ "aria-haspopup": "dialog",
400
+ "aria-expanded": openColor.value === item.key
401
+ }, [createElementVNode("span", _hoisted_4, [(openBlock(), createBlock(resolveDynamicComponent(item.icon))), createElementVNode("span", _hoisted_5, [item.color.none ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [
402
+ _cache[0] || (_cache[0] = createElementVNode("span", null, null, -1)),
403
+ _cache[1] || (_cache[1] = createElementVNode("span", null, null, -1)),
404
+ _cache[2] || (_cache[2] = createElementVNode("span", null, null, -1))
405
+ ], 64)) : createCommentVNode("", true)])])], 14, _hoisted_3)]),
406
+ _: 2
407
+ }, 1032, [
408
+ "open",
409
+ "placement",
410
+ "disabled",
411
+ "onUpdate:open"
412
+ ])]),
413
+ _: 2
414
+ }, 1032, [
415
+ "content",
416
+ "shortcut",
417
+ "disabled",
418
+ "placement"
419
+ ])) : (openBlock(), createBlock(unref(DouTooltip), {
420
+ key: 1,
130
421
  content: item.label,
131
422
  shortcut: item.shortcut,
132
423
  disabled: !!unref(menuFrom) && unref(menuFrom) === unref(buttons).get(item.key),
@@ -143,14 +434,14 @@ var flow_toolbar_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ def
143
434
  "aria-haspopup": opensMenu(item) ? "menu" : void 0,
144
435
  "aria-expanded": opensMenu(item) ? !!unref(menuFrom) && unref(menuFrom) === unref(buttons).get(item.key) : void 0,
145
436
  onClick: ($event) => press(item, $event)
146
- }, [(openBlock(), createBlock(resolveDynamicComponent(item.icon)))], 10, _hoisted_3)]),
437
+ }, [(openBlock(), createBlock(resolveDynamicComponent(item.icon)))], 10, _hoisted_15)]),
147
438
  _: 2
148
439
  }, 1032, [
149
440
  "content",
150
441
  "shortcut",
151
442
  "disabled",
152
443
  "placement"
153
- ]);
444
+ ]))], 64);
154
445
  }), 128))], 64);
155
446
  }), 128))], 12, _hoisted_1)), [[vShow, !hidden.value]]);
156
447
  };
@@ -1 +1 @@
1
- {"version":3,"file":"flow-toolbar.vue_vue_type_script_setup_true_lang.js","names":["_withDirectives","_openBlock","_createElementBlock","_unref","_normalizeStyle","_Fragment","_renderList","_createCommentVNode","_createBlock","_withCtx","_createElementVNode","_normalizeClass","_resolveDynamicComponent","_vShow"],"sources":["../../src/components/flow-toolbar.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport { DouTooltip } from 'dou-ui'\nimport { computed, ref, shallowReactive, useTemplateRef, watch } from 'vue'\nimport { useFlowEditor } from '../context'\nimport type { FlowToolbarItem } from '../core/menu'\n\nconst props = defineProps<{\n /** Each extension's buttons, with a divider between them. */\n groups: FlowToolbarItem[][]\n /** The selection on screen, in px from the editor's top left. */\n anchor: {\n x: number\n top: number\n bottom: number\n /** Boxes it should rather not cover, e.g. nodes beside selected edges, in the same px. */\n avoid?: { left: number; top: number; right: number; bottom: number }[]\n }\n size: { width: number; height: number }\n}>()\n\nconst { messages, root, showMenu, menuFrom } = useFlowEditor()\n\nconst opensMenu = (item: FlowToolbarItem) => !!item.children?.length\n\n// A button with `children` opens its choices under it; a click from the\n// keyboard (no pointer, `detail` 0) puts focus on the one in use.\nfunction press(item: FlowToolbarItem, event: MouseEvent) {\n if (!opensMenu(item)) return item.run?.()\n const button = event.currentTarget as HTMLElement\n const box = button.getBoundingClientRect()\n showMenu(item.children!, { x: box.left, y: box.bottom + 4 }, button, {\n focusChecked: event.detail === 0,\n })\n}\n// By key: extensions give new item objects on every update.\nconst warned = new Set<string>()\n/** Says once when a button does nothing when pressed. */\nfunction warnOnce(item: FlowToolbarItem) {\n // the app's bundler replaces this, as for Vue's own warnings\n if (process.env.NODE_ENV === 'production' || warned.has(item.key)) return\n if (item.run || opensMenu(item)) return\n warned.add(item.key)\n console.warn(\n `[@dou-ui/flow] the toolbar item \"${item.key}\" has no run or children: pressing it does nothing`,\n )\n}\nwatch(\n () => props.groups,\n (groups) => groups.flat().forEach(warnOnce),\n { immediate: true },\n)\n\nconst el = useTemplateRef<HTMLElement>('el')\nconst box = ref({ width: 0, height: 0 })\n\nwatch(\n [el, () => props.groups],\n () => {\n if (el.value) box.value = { width: el.value.offsetWidth, height: el.value.offsetHeight }\n },\n { flush: 'post' },\n)\n\n/** Room between the toolbar and the selection, and the editor's edges. */\nconst GAP = 8\n\n/*\n * What it keeps clear of is read from the page, so it is read again once the\n * page has caught up with the selection: a handle drawn for the selected node\n * appears in the same update as the toolbar's new anchor.\n */\nconst measured = ref(0)\nwatch(\n () => props.anchor,\n () => measured.value++,\n { flush: 'post', immediate: true },\n)\n\n/**\n * Boxes of what the toolbar must not cover, from the editor's top left: what\n * floats over the canvas, e.g. a whiteboard's tools, and handles on the\n * selection, e.g. a whiteboard's turn handle.\n */\nfunction overlays() {\n void measured.value\n const editor = root.value\n if (!editor) return []\n const origin = editor.getBoundingClientRect()\n return [...editor.querySelectorAll<HTMLElement>('[data-flow-overlay]')].map((item) => {\n const r = item.getBoundingClientRect()\n return {\n left: r.left - origin.left,\n top: r.top - origin.top,\n right: r.right - origin.left,\n bottom: r.bottom - origin.top,\n }\n })\n}\n\n// Above the selection, below it when there is no room or it would cover\n// something floating there, kept inside the editor.\nconst place = computed(() => {\n const { width, height } = box.value\n const { anchor, size } = props\n const left = Math.min(Math.max(anchor.x - width / 2, GAP), size.width - width - GAP)\n const above = anchor.top - GAP - height\n const under = anchor.bottom + GAP\n const boxes = [...overlays(), ...(anchor.avoid ?? [])]\n const hits = (top: number) =>\n boxes.filter(\n (o) => left < o.right && left + width > o.left && top < o.bottom && top + height > o.top,\n )\n const fits = (top: number) => top >= GAP && top + height <= size.height - GAP && !hits(top).length\n // Above, else below; when something is in the way, just past it.\n const candidates = [above, under]\n const over = hits(above)\n if (over.length) candidates.push(Math.min(...over.map((o) => o.top)) - GAP - height)\n const beneath = hits(under)\n if (beneath.length) candidates.push(Math.max(...beneath.map((o) => o.bottom)) + GAP)\n const top = candidates.find(fits) ?? (above >= GAP ? above : under)\n return { left, top: Math.min(Math.max(top, GAP), size.height - height - GAP) }\n})\nconst style = computed(() => ({\n left: `${Math.round(place.value.left)}px`,\n top: `${Math.round(place.value.top)}px`,\n}))\n/** Tips open away from the selection, so they never cover it. */\nconst tipPlacement = computed(() => (place.value.top < props.anchor.top ? 'top' : 'bottom'))\n\n/** The buttons by key, to tell which one opened the menu. */\nconst buttons = shallowReactive(new Map<string, Element>())\nfunction setButton(key: string, button: unknown) {\n if (button instanceof Element) buttons.set(key, button)\n else buttons.delete(key)\n}\n\n// The selection scrolled out of view: nothing to point at.\nconst hidden = computed(\n () =>\n props.anchor.bottom < 0 ||\n props.anchor.top > props.size.height ||\n props.anchor.x < 0 ||\n props.anchor.x > props.size.width,\n)\n</script>\n\n<template>\n <div\n v-show=\"!hidden\"\n ref=\"el\"\n class=\"dou-flow-toolbar\"\n role=\"toolbar\"\n :aria-label=\"messages.toolbar\"\n :style=\"style\"\n >\n <template v-for=\"(group, i) in groups\" :key=\"i\">\n <span v-if=\"i > 0\" class=\"dou-flow-controls__divider\" />\n <!-- The tip stands aside while the button's menu is open. -->\n <DouTooltip\n v-for=\"item in group\"\n :key=\"item.key\"\n :content=\"item.label\"\n :shortcut=\"item.shortcut\"\n :disabled=\"!!menuFrom && menuFrom === buttons.get(item.key)\"\n :placement=\"tipPlacement\"\n >\n <button\n :ref=\"(button) => setButton(item.key, button)\"\n type=\"button\"\n class=\"dou-flow-controls__button\"\n :class=\"{ 'is-active': item.pressed }\"\n :disabled=\"item.disabled\"\n :aria-label=\"item.label\"\n :aria-pressed=\"opensMenu(item) ? undefined : (item.pressed ?? undefined)\"\n :aria-haspopup=\"opensMenu(item) ? 'menu' : undefined\"\n :aria-expanded=\"\n opensMenu(item) ? !!menuFrom && menuFrom === buttons.get(item.key) : undefined\n \"\n @click=\"press(item, $event)\"\n >\n <component :is=\"item.icon\" />\n </button>\n </DouTooltip>\n </template>\n </div>\n</template>\n"],"mappings":";;;;;;;;;;;;;;;;;AAgEA,IAAM,MAAM;;;;;;;;;EA1DZ,MAAM,QAAQ;EAcd,MAAM,EAAE,UAAU,MAAM,UAAU,aAAa,cAAc;EAE7D,MAAM,aAAa,SAA0B,CAAC,CAAC,KAAK,UAAU;EAI9D,SAAS,MAAM,MAAuB,OAAmB;GACvD,IAAI,CAAC,UAAU,IAAI,GAAG,OAAO,KAAK,MAAM;GACxC,MAAM,SAAS,MAAM;GACrB,MAAM,MAAM,OAAO,sBAAsB;GACzC,SAAS,KAAK,UAAW;IAAE,GAAG,IAAI;IAAM,GAAG,IAAI,SAAS;GAAE,GAAG,QAAQ,EACnE,cAAc,MAAM,WAAW,EACjC,CAAC;EACH;EA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C,iBAA4B,UAAU,IAAI,IAAA,CAAA,CAAMR,MAAA,QAAA,KAAYA,MAAA,QAAA,MAAaA,MAAA,OAAA,CAAO,CAAC,IAAI,KAAK,GAAG,IAAI,KAAA;OAGjG,UAAK,WAAE,MAAM,MAAM,MAAM;M,KAE1BF,UAAA,GAAAO,YAA6BI,wBAAb,KAAK,IAAI,CAAA,E;;;;;;;;;G,+BAhCtB,CAAAC,OAAA,CAAA,OAAA,KAAM,C"}
1
+ {"version":3,"file":"flow-toolbar.vue_vue_type_script_setup_true_lang.js","names":["_withDirectives","_openBlock","_createElementBlock","_unref","_normalizeStyle","_Fragment","_renderList","_createCommentVNode","_createBlock","_withCtx","_createVNode","_createElementVNode","_normalizeClass","_resolveDynamicComponent","_toDisplayString","_vShow"],"sources":["../../src/components/flow-toolbar.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport { PaletteIcon } from '@dou-ui/icons'\nimport { DouColorPanel, DouPopover, DouTooltip } from 'dou-ui'\nimport {\n computed,\n nextTick,\n onBeforeUnmount,\n ref,\n shallowReactive,\n useTemplateRef,\n watch,\n} from 'vue'\nimport { useFlowEditor } from '../context'\nimport type { FlowToolbarColor, FlowToolbarItem } from '../core/menu'\nimport { plainColor } from '../export/color'\n\nconst props = defineProps<{\n /** Each extension's buttons, with a divider between them. */\n groups: FlowToolbarItem[][]\n /** The selection on screen, in px from the editor's top left. */\n anchor: {\n x: number\n top: number\n bottom: number\n /** Boxes it should rather not cover, e.g. nodes beside selected edges, in the same px. */\n avoid?: { left: number; top: number; right: number; bottom: number }[]\n }\n size: { width: number; height: number }\n}>()\n\nconst { messages, root, selection, showMenu, menuFrom } = useFlowEditor()\n\nconst opensMenu = (item: FlowToolbarItem) => !!item.children?.length\nconst opensColor = (item: FlowToolbarItem) => !!item.color\n\n// Color popover state -------------------------------------------------------------------\n\nconst openColor = ref<string | null>(null)\nconst customKey = ref<string | null>(null)\nconst customColor = ref<string | null>(null)\nconst customPlacement = ref<'right-start' | 'left-start' | 'bottom-end' | 'top-end'>('right-start')\nconst customOffset = ref(19)\nconst swatchesEls = new Map<string, HTMLElement>()\nlet closeActiveGroup: (() => void) | null = null\n\nfunction setSwatchesEl(key: string, el: unknown) {\n if (el instanceof HTMLElement) swatchesEls.set(key, el)\n else swatchesEls.delete(key)\n}\n\nfunction finishColorGroup() {\n closeActiveGroup?.()\n closeActiveGroup = null\n}\n\nfunction closeColorPopover() {\n finishColorGroup()\n openColor.value = null\n customKey.value = null\n}\n\nfunction isCustomColor(color: FlowToolbarColor) {\n const value = color.value\n if (!value || color.none || !color.swatch) return false\n return !color.presets.some((preset) => preset.value === value)\n}\n\nfunction customSwatch(color: FlowToolbarColor) {\n return isCustomColor(color) ? color.swatch : undefined\n}\n\nfunction setColorOpen(key: string, open: boolean) {\n if (open) {\n if (openColor.value && openColor.value !== key) {\n finishColorGroup()\n customKey.value = null\n }\n openColor.value = key\n } else if (openColor.value === key) {\n closeColorPopover()\n }\n}\n\nfunction pickColor(color: FlowToolbarColor, value: string | undefined) {\n finishColorGroup()\n color.select(value, { live: false })\n openColor.value = null\n customKey.value = null\n void nextTick(() => root.value?.focus({ preventScroll: true }))\n}\n\nconst CSS_COLOR = /^(#|rgb|hsl|hsv|hwb|lab|lch|oklab|oklch)/i\nconst CUSTOM_PANEL_WIDTH = 258\nconst CUSTOM_PANEL_HEIGHT = 260\nconst CUSTOM_POPOVER_GAP = 8\nconst VIEWPORT_PADDING = 8\n\n/** Place the secondary color picker cleanly beside (or above/below) the primary swatch popover without overlapping it. */\nfunction placeCustomPopover(swatches: HTMLElement | undefined) {\n const primaryPanel = swatches?.closest<HTMLElement>('.dou-flow-color-popover')\n const customButton = swatches?.querySelector<HTMLElement>('.is-custom')\n if (!primaryPanel || !customButton) {\n customPlacement.value = 'right-start'\n customOffset.value = 19\n return\n }\n const panelRect = primaryPanel.getBoundingClientRect()\n const btnRect = customButton.getBoundingClientRect()\n const vw = document.documentElement.clientWidth\n const vh = document.documentElement.clientHeight\n const roomRight = vw - panelRect.right - VIEWPORT_PADDING\n const roomLeft = panelRect.left - VIEWPORT_PADDING\n if (roomRight >= CUSTOM_PANEL_WIDTH + CUSTOM_POPOVER_GAP) {\n customPlacement.value = 'right-start'\n customOffset.value = Math.round(panelRect.right - btnRect.right + CUSTOM_POPOVER_GAP)\n } else if (roomLeft >= CUSTOM_PANEL_WIDTH + CUSTOM_POPOVER_GAP) {\n customPlacement.value = 'left-start'\n customOffset.value = Math.round(btnRect.left - panelRect.left + CUSTOM_POPOVER_GAP)\n } else {\n const roomBelow = vh - panelRect.bottom - VIEWPORT_PADDING\n const roomAbove = panelRect.top - VIEWPORT_PADDING\n if (roomBelow < CUSTOM_PANEL_HEIGHT + CUSTOM_POPOVER_GAP && roomAbove > roomBelow) {\n customPlacement.value = 'top-end'\n customOffset.value = Math.round(btnRect.top - panelRect.top + CUSTOM_POPOVER_GAP)\n } else {\n customPlacement.value = 'bottom-end'\n customOffset.value = Math.round(panelRect.bottom - btnRect.bottom + CUSTOM_POPOVER_GAP)\n }\n }\n}\n\nfunction setCustomOpen(item: FlowToolbarItem, open: boolean) {\n const color = item.color\n if (!color) return\n if (!open) {\n if (customKey.value === item.key) {\n finishColorGroup()\n customKey.value = null\n }\n return\n }\n if (customKey.value && customKey.value !== item.key) finishColorGroup()\n const swatches = swatchesEls.get(item.key)\n placeCustomPopover(swatches)\n customKey.value = item.key\n if (isCustomColor(color) && color.value && CSS_COLOR.test(color.value)) {\n customColor.value = color.value\n return\n }\n // Start from the active swatch's computed color so tweaking a preset or tone begins near it.\n const active = swatches?.querySelector<HTMLElement>('[aria-pressed=\"true\"]:not(.is-default)')\n const raw = active && getComputedStyle(active).color\n customColor.value = raw ? plainColor(raw) || null : null\n}\n\nfunction onCustomInput(color: FlowToolbarColor, value: string | null | undefined) {\n customColor.value = value ?? null\n if (value) {\n closeActiveGroup = color.close ?? null\n color.select(value, { live: true })\n }\n}\n\nfunction onCustomChange(color: FlowToolbarColor, value: string | null | undefined) {\n customColor.value = value ?? null\n if (value) color.select(value, { live: false })\n closeActiveGroup = null\n}\n\n// Close open color popover when a context menu opens, selection changes, or the button vanishes.\nwatch(menuFrom, (el) => {\n if (el) closeColorPopover()\n})\nwatch(\n () => [selection.value.nodes.join(','), selection.value.edges.join(',')],\n () => closeColorPopover(),\n)\nwatch(\n () => props.groups,\n (groups) => {\n if (openColor.value && !groups.flat().some((item) => item.key === openColor.value)) {\n closeColorPopover()\n }\n },\n)\nonBeforeUnmount(() => finishColorGroup())\n\n// A button with `children` opens its choices under it; a click from the\n// keyboard (no pointer, `detail` 0) puts focus on the one in use.\nfunction press(item: FlowToolbarItem, event: MouseEvent) {\n closeColorPopover()\n if (!opensMenu(item)) return item.run?.()\n const button = event.currentTarget as HTMLElement\n const box = button.getBoundingClientRect()\n showMenu(item.children!, { x: box.left, y: box.bottom + 4 }, button, {\n focusChecked: event.detail === 0,\n })\n}\n// By key: extensions give new item objects on every update.\nconst warned = new Set<string>()\n/** Says once when a button does nothing when pressed. */\nfunction warnOnce(item: FlowToolbarItem) {\n // the app's bundler replaces this, as for Vue's own warnings\n if (process.env.NODE_ENV === 'production' || warned.has(item.key)) return\n if (item.run || opensMenu(item) || opensColor(item)) return\n warned.add(item.key)\n console.warn(\n `[@dou-ui/flow] the toolbar item \"${item.key}\" has no run, children or color: pressing it does nothing`,\n )\n}\nwatch(\n () => props.groups,\n (groups) => groups.flat().forEach(warnOnce),\n { immediate: true },\n)\n\nconst el = useTemplateRef<HTMLElement>('el')\nconst box = ref({ width: 0, height: 0 })\n\nwatch(\n [el, () => props.groups],\n () => {\n if (el.value) box.value = { width: el.value.offsetWidth, height: el.value.offsetHeight }\n },\n { flush: 'post' },\n)\n\n/** Room between the toolbar and the selection, and the editor's edges. */\nconst GAP = 8\n\n/*\n * What it keeps clear of is read from the page, so it is read again once the\n * page has caught up with the selection: a handle drawn for the selected node\n * appears in the same update as the toolbar's new anchor.\n */\nconst measured = ref(0)\nwatch(\n () => props.anchor,\n () => measured.value++,\n { flush: 'post', immediate: true },\n)\n\n/**\n * Boxes of what the toolbar must not cover, from the editor's top left: what\n * floats over the canvas, e.g. a whiteboard's tools, and handles on the\n * selection, e.g. a whiteboard's turn handle.\n */\nfunction overlays() {\n void measured.value\n const editor = root.value\n if (!editor) return []\n const origin = editor.getBoundingClientRect()\n return [...editor.querySelectorAll<HTMLElement>('[data-flow-overlay]')].map((item) => {\n const r = item.getBoundingClientRect()\n return {\n left: r.left - origin.left,\n top: r.top - origin.top,\n right: r.right - origin.left,\n bottom: r.bottom - origin.top,\n }\n })\n}\n\n// Above the selection, below it when there is no room or it would cover\n// something floating there, kept inside the editor.\nconst place = computed(() => {\n const { width, height } = box.value\n const { anchor, size } = props\n const left = Math.min(Math.max(anchor.x - width / 2, GAP), size.width - width - GAP)\n const above = anchor.top - GAP - height\n const under = anchor.bottom + GAP\n const boxes = [...overlays(), ...(anchor.avoid ?? [])]\n const hits = (top: number) =>\n boxes.filter(\n (o) => left < o.right && left + width > o.left && top < o.bottom && top + height > o.top,\n )\n const fits = (top: number) => top >= GAP && top + height <= size.height - GAP && !hits(top).length\n // Above, else below; when something is in the way, just past it.\n const candidates = [above, under]\n const over = hits(above)\n if (over.length) candidates.push(Math.min(...over.map((o) => o.top)) - GAP - height)\n const beneath = hits(under)\n if (beneath.length) candidates.push(Math.max(...beneath.map((o) => o.bottom)) + GAP)\n const top = candidates.find(fits) ?? (above >= GAP ? above : under)\n return { left, top: Math.min(Math.max(top, GAP), size.height - height - GAP) }\n})\nconst style = computed(() => ({\n left: `${Math.round(place.value.left)}px`,\n top: `${Math.round(place.value.top)}px`,\n}))\n/** Tips open away from the selection, so they never cover it. */\nconst tipPlacement = computed(() => (place.value.top < props.anchor.top ? 'top' : 'bottom'))\nconst popoverPlacement = computed(() =>\n place.value.top < props.anchor.top ? 'top-start' : 'bottom-start',\n)\n\n/** The buttons by key, to tell which one opened the menu. */\nconst buttons = shallowReactive(new Map<string, Element>())\nfunction setButton(key: string, button: unknown) {\n if (button instanceof Element) buttons.set(key, button)\n else buttons.delete(key)\n}\n\n// The selection scrolled out of view: nothing to point at.\nconst hidden = computed(\n () =>\n props.anchor.bottom < 0 ||\n props.anchor.top > props.size.height ||\n props.anchor.x < 0 ||\n props.anchor.x > props.size.width,\n)\nwatch(hidden, (isHidden) => {\n if (isHidden) closeColorPopover()\n})\n</script>\n\n<template>\n <div\n v-show=\"!hidden\"\n ref=\"el\"\n class=\"dou-flow-toolbar\"\n role=\"toolbar\"\n :aria-label=\"messages.toolbar\"\n :style=\"style\"\n >\n <template v-for=\"(group, i) in groups\" :key=\"i\">\n <span v-if=\"i > 0\" class=\"dou-flow-controls__divider\" />\n <template v-for=\"item in group\" :key=\"item.key\">\n <DouTooltip\n v-if=\"item.color\"\n :content=\"item.label\"\n :shortcut=\"item.shortcut\"\n :disabled=\"openColor === item.key\"\n :placement=\"tipPlacement\"\n >\n <DouPopover\n :open=\"openColor === item.key\"\n :placement=\"popoverPlacement\"\n :disabled=\"item.disabled\"\n class=\"dou-flow-color-popover\"\n @update:open=\"setColorOpen(item.key, $event)\"\n >\n <button\n :ref=\"(button) => setButton(item.key, button)\"\n type=\"button\"\n class=\"dou-flow-controls__button dou-flow-toolbar__color\"\n :class=\"[\n `is-kind-${item.color.kind ?? 'stroke'}`,\n {\n 'is-active': openColor === item.key,\n 'has-swatch': !!item.color.swatch,\n 'is-none': !!item.color.none,\n },\n ]\"\n :style=\"item.color.swatch ? { '--dou-flow-swatch': item.color.swatch } : undefined\"\n :disabled=\"item.disabled\"\n :aria-label=\"item.label\"\n aria-haspopup=\"dialog\"\n :aria-expanded=\"openColor === item.key\"\n >\n <span class=\"dou-flow-toolbar__color-glyph\">\n <component :is=\"item.icon\" />\n <span class=\"dou-flow-toolbar__color-bar\" aria-hidden=\"true\">\n <template v-if=\"item.color.none\"> <span /><span /><span /> </template>\n </span>\n </span>\n </button>\n <template #content>\n <div class=\"dou-flow-color-popover__body\">\n <div\n :ref=\"(el) => setSwatchesEl(item.key, el)\"\n class=\"dou-flow-color-popover__swatches\"\n role=\"group\"\n :aria-label=\"item.label\"\n >\n <button\n type=\"button\"\n class=\"dou-flow-color-popover__swatch is-default\"\n :class=\"`is-kind-${item.color.kind ?? 'stroke'}`\"\n :aria-pressed=\"item.color.value === undefined\"\n :aria-label=\"item.color.defaultLabel\"\n :title=\"item.color.defaultLabel\"\n @click=\"pickColor(item.color, undefined)\"\n />\n <button\n v-for=\"preset in item.color.presets\"\n :key=\"preset.value\"\n type=\"button\"\n class=\"dou-flow-color-popover__swatch\"\n :class=\"`is-kind-${item.color.kind ?? 'stroke'}`\"\n :style=\"{ '--dou-flow-swatch': preset.swatch }\"\n :aria-pressed=\"item.color.value === preset.value\"\n :aria-label=\"preset.label\"\n :title=\"preset.label\"\n @click=\"pickColor(item.color, preset.value)\"\n >\n <span v-if=\"item.color.kind === 'text'\" aria-hidden=\"true\">A</span>\n </button>\n <DouPopover\n :open=\"customKey === item.key\"\n :placement=\"customPlacement\"\n :offset=\"customOffset\"\n class=\"dou-flow-color-custom-popover\"\n @update:open=\"setCustomOpen(item, $event)\"\n >\n <button\n type=\"button\"\n class=\"dou-flow-color-popover__swatch is-custom\"\n :class=\"[\n `is-kind-${item.color.kind ?? 'stroke'}`,\n { 'has-custom': isCustomColor(item.color) },\n ]\"\n :style=\"\n customSwatch(item.color)\n ? { '--dou-flow-swatch': customSwatch(item.color) }\n : undefined\n \"\n :aria-pressed=\"isCustomColor(item.color)\"\n :aria-label=\"item.color.customLabel\"\n :title=\"item.color.customLabel\"\n >\n <span\n v-if=\"isCustomColor(item.color) && item.color.kind === 'text'\"\n aria-hidden=\"true\"\n >\n A\n </span>\n <PaletteIcon v-else-if=\"!isCustomColor(item.color)\" />\n </button>\n <template #content>\n <DouColorPanel\n :model-value=\"customKey === item.key ? customColor : null\"\n :bordered=\"false\"\n format=\"hex\"\n show-alpha\n show-input\n @update:model-value=\"onCustomInput(item.color, $event)\"\n @change=\"onCustomChange(item.color, $event)\"\n />\n </template>\n </DouPopover>\n </div>\n <button\n v-if=\"item.color.noneOption\"\n type=\"button\"\n class=\"dou-flow-color-popover__none\"\n :aria-pressed=\"!!item.color.none\"\n @click=\"pickColor(item.color, item.color.noneOption.value)\"\n >\n <component\n :is=\"item.color.noneOption.icon\"\n v-if=\"item.color.noneOption.icon\"\n class=\"dou-flow-color-popover__none-icon\"\n />\n <span v-else class=\"dou-flow-color-popover__none-swatch\" aria-hidden=\"true\" />\n <span>{{ item.color.noneOption.label }}</span>\n </button>\n </div>\n </template>\n </DouPopover>\n </DouTooltip>\n <!-- The tip stands aside while the button's menu is open. -->\n <DouTooltip\n v-else\n :content=\"item.label\"\n :shortcut=\"item.shortcut\"\n :disabled=\"!!menuFrom && menuFrom === buttons.get(item.key)\"\n :placement=\"tipPlacement\"\n >\n <button\n :ref=\"(button) => setButton(item.key, button)\"\n type=\"button\"\n class=\"dou-flow-controls__button\"\n :class=\"{ 'is-active': item.pressed }\"\n :disabled=\"item.disabled\"\n :aria-label=\"item.label\"\n :aria-pressed=\"opensMenu(item) ? undefined : (item.pressed ?? undefined)\"\n :aria-haspopup=\"opensMenu(item) ? 'menu' : undefined\"\n :aria-expanded=\"\n opensMenu(item) ? !!menuFrom && menuFrom === buttons.get(item.key) : undefined\n \"\n @click=\"press(item, $event)\"\n >\n <component :is=\"item.icon\" />\n </button>\n </DouTooltip>\n </template>\n </template>\n </div>\n</template>\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8FA,IAAM,qBAAqB;AAC3B,IAAM,mBAAmB;;AAqIzB,IAAM,MAAM;;;;;;;;;EApNZ,MAAM,QAAQ;EAcd,MAAM,EAAE,UAAU,MAAM,WAAW,UAAU,aAAa,cAAc;EAExE,MAAM,aAAa,SAA0B,CAAC,CAAC,KAAK,UAAU;EAC9D,MAAM,cAAc,SAA0B,CAAC,CAAC,KAAK;EAIrD,MAAM,YAAY,IAAmB,IAAI;EACzC,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AAAE,OAAA,CAAA,OAAA,KAAM,C"}
@@ -1,16 +1,54 @@
1
1
  // src/export/color.ts
2
2
  var normalizer;
3
- var SRGB = /^(#|rgba?\()/;
3
+ var HEX = /^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
4
+ var LEGACY_RGB = /^rgba?\(\s*[\d.+%-]+\s*,\s*[\d.+%-]+\s*,\s*[\d.+%-]+(?:\s*,\s*[\d.+%-]+)?\s*\)$/i;
5
+ var MODERN_RGB = /^rgba?\(\s*(?:[\d.+%-]+|none)\s+(?:[\d.+%-]+|none)\s+(?:[\d.+%-]+|none)(?:\s*\/\s*(?:[\d.+%-]+|none))?\s*\)$/i;
6
+ var CSS_WIDE = /^(?:inherit|initial|unset|revert|revert-layer)$/i;
7
+ var SAFE_CHARS = /^[a-z0-9#(),.%/+\-\s]+$/i;
8
+ var UNSAFE_FUNC = /\b(?:url|expression)\s*\(/i;
9
+ var COLOR_FUNC = /^(?:hsla?|hwb|lab|lch|oklab|oklch|color|color-mix|light-dark)\((.*)\)$/i;
10
+ var VAR_FUNC = /^var\(\s*--[a-z0-9_-]+(?:\s*,.*)?\)$/i;
11
+ var NAMED_COLOR = /^[a-z]+$/i;
12
+ var SRGB = (value) => HEX.test(value) || LEGACY_RGB.test(value);
13
+ function hasBalancedParens(value) {
14
+ let depth = 0;
15
+ for (let i = 0; i < value.length; i++) {
16
+ const ch = value[i];
17
+ if (ch === "(") depth++;
18
+ else if (ch === ")") {
19
+ depth--;
20
+ if (depth < 0) return false;
21
+ if (depth === 0 && i !== value.length - 1) return false;
22
+ }
23
+ }
24
+ return depth === 0;
25
+ }
26
+ /**
27
+ * Whether `value` has safe CSS color syntax (no quotes, markup, CSS
28
+ * declarations, `url()`, or CSS-wide keywords like `inherit`).
29
+ */
30
+ function isSafeCssColor(value) {
31
+ const trimmed = value.trim();
32
+ if (!trimmed || trimmed.toLowerCase() === "none" || CSS_WIDE.test(trimmed)) return false;
33
+ if (!SAFE_CHARS.test(trimmed) || UNSAFE_FUNC.test(trimmed) || !hasBalancedParens(trimmed)) return false;
34
+ if (trimmed.startsWith("#")) return HEX.test(trimmed);
35
+ if (/^rgba?\(/i.test(trimmed)) return LEGACY_RGB.test(trimmed) || MODERN_RGB.test(trimmed);
36
+ if (VAR_FUNC.test(trimmed)) return true;
37
+ if (COLOR_FUNC.test(trimmed)) return true;
38
+ return NAMED_COLOR.test(trimmed);
39
+ }
4
40
  /**
5
41
  * A computed color as `rgb(…)`, `rgba(…)` or `#rrggbb`, which every SVG reader
6
42
  * understands. A canvas converts most colors as it parses them; colors it
7
43
  * keeps in another space (`oklab(…)`, `color(…)`) are painted on a pixel
8
- * and read back.
44
+ * and read back. Invalid or unsafe values return `''`.
9
45
  */
10
46
  function plainColor(color) {
11
47
  const value = color.trim();
12
- if (!value || value === "none" || SRGB.test(value)) return value;
13
- if (value === "transparent") return "rgba(0, 0, 0, 0)";
48
+ if (!value || value === "none") return value;
49
+ if (SRGB(value)) return value;
50
+ if (value.toLowerCase() === "transparent") return "rgba(0, 0, 0, 0)";
51
+ if (!isSafeCssColor(value)) return "";
14
52
  if (normalizer === void 0) try {
15
53
  const sheet = document.createElement("canvas");
16
54
  sheet.width = sheet.height = 1;
@@ -22,7 +60,12 @@ function plainColor(color) {
22
60
  normalizer.fillStyle = "#000";
23
61
  normalizer.fillStyle = value;
24
62
  const parsed = String(normalizer.fillStyle);
25
- if (SRGB.test(parsed)) return parsed;
63
+ if (parsed === "#000000") {
64
+ normalizer.fillStyle = "#fff";
65
+ normalizer.fillStyle = value;
66
+ if (String(normalizer.fillStyle) === "#ffffff") return "";
67
+ }
68
+ if (SRGB(parsed)) return parsed;
26
69
  normalizer.clearRect(0, 0, 1, 1);
27
70
  normalizer.fillRect(0, 0, 1, 1);
28
71
  const [r, g, b, a] = normalizer.getImageData(0, 0, 1, 1).data;
@@ -33,6 +76,6 @@ function plainColor(color) {
33
76
  ].map((n) => n.toString(16).padStart(2, "0")).join("")}`;
34
77
  return `rgba(${r}, ${g}, ${b}, ${Math.round(a / 255 * 1e3) / 1e3})`;
35
78
  }
36
- export { plainColor };
79
+ export { isSafeCssColor, plainColor };
37
80
 
38
81
  //# sourceMappingURL=color.js.map