@8wave/ai-elements 0.125.0-beta.32 → 0.125.0-beta.33

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 (103) hide show
  1. package/dist/_chunks/{PkChatbotFilePreview-CqxtVySb.js → PkChatbotFilePreview-DsRAk1js.js} +2 -2
  2. package/dist/_chunks/{PkChatbotFilePreview-CqxtVySb.js.map → PkChatbotFilePreview-DsRAk1js.js.map} +1 -1
  3. package/dist/_chunks/PkImageEditorDialog-BQwM06kT.js +939 -0
  4. package/dist/_chunks/PkImageEditorDialog-BQwM06kT.js.map +1 -0
  5. package/dist/_chunks/{PkStreamingMarkdown-BqDooeUU.js → PkStreamingMarkdown-CjL7l_3E.js} +2 -2
  6. package/dist/_chunks/{PkStreamingMarkdown-BqDooeUU.js.map → PkStreamingMarkdown-CjL7l_3E.js.map} +1 -1
  7. package/dist/_chunks/{PkToolChatFile-DpStX3wS.js → PkToolChatFile-weptM8iA.js} +2 -2
  8. package/dist/_chunks/{PkToolChatFile-DpStX3wS.js.map → PkToolChatFile-weptM8iA.js.map} +1 -1
  9. package/dist/_chunks/{PkToolShareDocument-BvHklbzb.js → PkToolShareDocument-D5K3uJ2x.js} +2 -2
  10. package/dist/_chunks/{PkToolShareDocument-BvHklbzb.js.map → PkToolShareDocument-D5K3uJ2x.js.map} +1 -1
  11. package/dist/_chunks/{PkToolShowArtifact-YPwJLLlW.js → PkToolShowArtifact-Bm2zS5YS.js} +2 -2
  12. package/dist/_chunks/{PkToolShowArtifact-YPwJLLlW.js.map → PkToolShowArtifact-Bm2zS5YS.js.map} +1 -1
  13. package/dist/_chunks/{PkToolShowContactForm-CTc1mbA8.js → PkToolShowContactForm-BKJCv5To.js} +2 -2
  14. package/dist/_chunks/{PkToolShowContactForm-CTc1mbA8.js.map → PkToolShowContactForm-BKJCv5To.js.map} +1 -1
  15. package/dist/_chunks/{PkToolShowDiagram-COt1Z3q9.js → PkToolShowDiagram-jpaEAi2X.js} +2 -2
  16. package/dist/_chunks/{PkToolShowDiagram-COt1Z3q9.js.map → PkToolShowDiagram-jpaEAi2X.js.map} +1 -1
  17. package/dist/_chunks/{PkToolShowForm-DT5EyYVY.js → PkToolShowForm-DvAcJgDP.js} +2 -2
  18. package/dist/_chunks/{PkToolShowForm-DT5EyYVY.js.map → PkToolShowForm-DvAcJgDP.js.map} +1 -1
  19. package/dist/_chunks/{PkToolShowImageGallery-DKrCrrB5.js → PkToolShowImageGallery-D0htuXNL.js} +4 -4
  20. package/dist/_chunks/{PkToolShowImageGallery-DKrCrrB5.js.map → PkToolShowImageGallery-D0htuXNL.js.map} +1 -1
  21. package/dist/_chunks/{PkToolShowSources-CzSknPo6.js → PkToolShowSources-Cfj6iRGj.js} +2 -2
  22. package/dist/_chunks/{PkToolShowSources-CzSknPo6.js.map → PkToolShowSources-Cfj6iRGj.js.map} +1 -1
  23. package/dist/_chunks/{useLightbox-D2Zi-Fpe.js → useLightbox-C54BCSbU.js} +130 -89
  24. package/dist/_chunks/useLightbox-C54BCSbU.js.map +1 -0
  25. package/dist/ai-elements.es.js +2774 -2721
  26. package/dist/ai-elements.es.js.map +1 -1
  27. package/dist-vue/PkChatbot.js +1 -1
  28. package/dist-vue/PkChatbotEmptyState.js +1 -1
  29. package/dist-vue/PkChatbotFilePreview.js +1 -1
  30. package/dist-vue/PkChatbotInput.js +1 -1
  31. package/dist-vue/PkChatbotMessages.js +1 -1
  32. package/dist-vue/PkChatbotSteps.js +1 -1
  33. package/dist-vue/PkChatbotViewChat.js +1 -1
  34. package/dist-vue/_chunks/{PkChatbot-g-mj6Luo.js → PkChatbot-Bc2hlwFO.js} +3 -3
  35. package/dist-vue/_chunks/{PkChatbot-g-mj6Luo.js.map → PkChatbot-Bc2hlwFO.js.map} +1 -1
  36. package/dist-vue/_chunks/{PkChatbotEmptyState-DMPEj1Qa.js → PkChatbotEmptyState-NHarCaDm.js} +2 -2
  37. package/dist-vue/_chunks/{PkChatbotEmptyState-DMPEj1Qa.js.map → PkChatbotEmptyState-NHarCaDm.js.map} +1 -1
  38. package/dist-vue/_chunks/{PkChatbotFilePreview-j_8sDZUE.js → PkChatbotFilePreview-BIta8Yg-.js} +2 -2
  39. package/dist-vue/_chunks/{PkChatbotFilePreview-j_8sDZUE.js.map → PkChatbotFilePreview-BIta8Yg-.js.map} +1 -1
  40. package/dist-vue/_chunks/{PkChatbotInput-OBoyFkZ-.js → PkChatbotInput-BSzDRoDD.js} +192 -181
  41. package/dist-vue/_chunks/PkChatbotInput-BSzDRoDD.js.map +1 -0
  42. package/dist-vue/_chunks/{PkChatbotMessages-bPBOSAv7.js → PkChatbotMessages-DEMKZDCb.js} +17 -17
  43. package/dist-vue/_chunks/{PkChatbotMessages-bPBOSAv7.js.map → PkChatbotMessages-DEMKZDCb.js.map} +1 -1
  44. package/dist-vue/_chunks/{PkChatbotSteps-pibgqxus.js → PkChatbotSteps-DGQoYxJ9.js} +2 -2
  45. package/dist-vue/_chunks/{PkChatbotSteps-pibgqxus.js.map → PkChatbotSteps-DGQoYxJ9.js.map} +1 -1
  46. package/dist-vue/_chunks/{PkChatbotViewChat-C28g7dz-.js → PkChatbotViewChat-aKi08vCz.js} +440 -431
  47. package/dist-vue/_chunks/PkChatbotViewChat-aKi08vCz.js.map +1 -0
  48. package/dist-vue/_chunks/{PkFilePreview-BgyJngDt.js → PkFilePreview-D2HInDoA.js} +2 -2
  49. package/dist-vue/_chunks/{PkFilePreview-BgyJngDt.js.map → PkFilePreview-D2HInDoA.js.map} +1 -1
  50. package/dist-vue/_chunks/{PkFilePreviewDialog-B3-oF1_R.js → PkFilePreviewDialog-jxcUSzPx.js} +2 -2
  51. package/dist-vue/_chunks/{PkFilePreviewDialog-B3-oF1_R.js.map → PkFilePreviewDialog-jxcUSzPx.js.map} +1 -1
  52. package/dist-vue/_chunks/PkImageEditorDialog-Dn3HV_ny.js +934 -0
  53. package/dist-vue/_chunks/PkImageEditorDialog-Dn3HV_ny.js.map +1 -0
  54. package/dist-vue/_chunks/{PkStreamingMarkdown-B3VWOaWb.js → PkStreamingMarkdown-DdOv86Eb.js} +11 -11
  55. package/dist-vue/_chunks/{PkStreamingMarkdown-B3VWOaWb.js.map → PkStreamingMarkdown-DdOv86Eb.js.map} +1 -1
  56. package/dist-vue/_chunks/{PkSvgViewer-BsdSOBpH.js → PkSvgViewer-vkd5Dluw.js} +2 -2
  57. package/dist-vue/_chunks/{PkSvgViewer-BsdSOBpH.js.map → PkSvgViewer-vkd5Dluw.js.map} +1 -1
  58. package/dist-vue/_chunks/{PkToolChatFile-PXpiJLZ6.js → PkToolChatFile-8nx166iM.js} +2 -2
  59. package/dist-vue/_chunks/{PkToolChatFile-PXpiJLZ6.js.map → PkToolChatFile-8nx166iM.js.map} +1 -1
  60. package/dist-vue/_chunks/{PkToolShareDocument-cMUqik6v.js → PkToolShareDocument-CspvFwRR.js} +2 -2
  61. package/dist-vue/_chunks/{PkToolShareDocument-cMUqik6v.js.map → PkToolShareDocument-CspvFwRR.js.map} +1 -1
  62. package/dist-vue/_chunks/{PkToolShowArtifact-DouCz_dZ.js → PkToolShowArtifact-BppP_xSN.js} +2 -2
  63. package/dist-vue/_chunks/{PkToolShowArtifact-DouCz_dZ.js.map → PkToolShowArtifact-BppP_xSN.js.map} +1 -1
  64. package/dist-vue/_chunks/{PkToolShowContactForm-BmiNlFfx.js → PkToolShowContactForm-Bm0icz24.js} +2 -2
  65. package/dist-vue/_chunks/{PkToolShowContactForm-BmiNlFfx.js.map → PkToolShowContactForm-Bm0icz24.js.map} +1 -1
  66. package/dist-vue/_chunks/{PkToolShowDiagram-D6mYTDU-.js → PkToolShowDiagram-CgukB1Ss.js} +2 -2
  67. package/dist-vue/_chunks/{PkToolShowDiagram-D6mYTDU-.js.map → PkToolShowDiagram-CgukB1Ss.js.map} +1 -1
  68. package/dist-vue/_chunks/{PkToolShowForm-CksTiimD.js → PkToolShowForm-CyyN7GQP.js} +2 -2
  69. package/dist-vue/_chunks/{PkToolShowForm-CksTiimD.js.map → PkToolShowForm-CyyN7GQP.js.map} +1 -1
  70. package/dist-vue/_chunks/{PkToolShowImageGallery-B-lVY0xH.js → PkToolShowImageGallery-zQF_lsxI.js} +5 -5
  71. package/dist-vue/_chunks/{PkToolShowImageGallery-B-lVY0xH.js.map → PkToolShowImageGallery-zQF_lsxI.js.map} +1 -1
  72. package/dist-vue/_chunks/{PkToolShowLocation-B8FE_Sqd.js → PkToolShowLocation-WdRqG8Wi.js} +2 -2
  73. package/dist-vue/_chunks/{PkToolShowLocation-B8FE_Sqd.js.map → PkToolShowLocation-WdRqG8Wi.js.map} +1 -1
  74. package/dist-vue/_chunks/{PkToolShowProductList-Dcq3Kdse.js → PkToolShowProductList-DW_P0gC2.js} +2 -2
  75. package/dist-vue/_chunks/{PkToolShowProductList-Dcq3Kdse.js.map → PkToolShowProductList-DW_P0gC2.js.map} +1 -1
  76. package/dist-vue/_chunks/{PkToolShowSources-DApWmwvk.js → PkToolShowSources-BugMybUx.js} +2 -2
  77. package/dist-vue/_chunks/{PkToolShowSources-DApWmwvk.js.map → PkToolShowSources-BugMybUx.js.map} +1 -1
  78. package/dist-vue/_chunks/{PkToolShowVideo-CjrYh9-q.js → PkToolShowVideo-BW7aRKs_.js} +2 -2
  79. package/dist-vue/_chunks/{PkToolShowVideo-CjrYh9-q.js.map → PkToolShowVideo-BW7aRKs_.js.map} +1 -1
  80. package/dist-vue/_chunks/{PkToolShowWebPages-DSIAizw-.js → PkToolShowWebPages-DxKimT-2.js} +2 -2
  81. package/dist-vue/_chunks/{PkToolShowWebPages-DSIAizw-.js.map → PkToolShowWebPages-DxKimT-2.js.map} +1 -1
  82. package/dist-vue/_chunks/{useLightbox-FDyQrPWe.js → useLightbox-Ctl9zaMX.js} +132 -91
  83. package/dist-vue/_chunks/useLightbox-Ctl9zaMX.js.map +1 -0
  84. package/dist-vue/composables.js +36 -2
  85. package/dist-vue/composables.js.map +1 -1
  86. package/dist-vue/index.js +22 -22
  87. package/dist-vue/index.js.map +1 -1
  88. package/dist-vue/locales.js +34 -0
  89. package/dist-vue/packages/components/src/chat/PkChatbotInput.d.ts +5 -0
  90. package/dist-vue/packages/components/src/chat/PkImageEditor.d.ts +11 -9
  91. package/dist-vue/packages/components/src/chat/PkImageEditorDialog.d.ts +12 -5
  92. package/dist-vue/packages/components/src/chat/imageEdits.d.ts +77 -1
  93. package/dist-vue/packages/components/src/composables/useLightbox.d.ts +26 -0
  94. package/dist-vue/style.css +1 -1
  95. package/package.json +1 -1
  96. package/dist/_chunks/PkImageEditorDialog-BMCnqiqI.js +0 -555
  97. package/dist/_chunks/PkImageEditorDialog-BMCnqiqI.js.map +0 -1
  98. package/dist/_chunks/useLightbox-D2Zi-Fpe.js.map +0 -1
  99. package/dist-vue/_chunks/PkChatbotInput-OBoyFkZ-.js.map +0 -1
  100. package/dist-vue/_chunks/PkChatbotViewChat-C28g7dz-.js.map +0 -1
  101. package/dist-vue/_chunks/PkImageEditorDialog-3L4URgPw.js +0 -550
  102. package/dist-vue/_chunks/PkImageEditorDialog-3L4URgPw.js.map +0 -1
  103. package/dist-vue/_chunks/useLightbox-FDyQrPWe.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PkImageEditorDialog-Dn3HV_ny.js","names":["_hoisted_1","_hoisted_2","_useModel","__unplugin_components_0","__unplugin_components_1","__unplugin_components_2","_openBlock","_createElementBlock","_createElementVNode","_createVNode","PkEditorRadioButtons","_unref","_createSlots","_renderList","_withCtx","_toDisplayString","_Fragment","_normalizeStyle","_normalizeClass","_createCommentVNode","_withDirectives","_withModifiers","_withKeys","_vModelText","_sfc_main","__unplugin_components_0","__unplugin_components_1","__unplugin_components_2","__unplugin_components_3","__unplugin_components_4","_openBlock","_createBlock","_unref","_withCtx","_createVNode","_createCommentVNode","PkImageEditor","_createElementBlock","_toDisplayString","_sfc_main","PkEditorRadioButtons_default","ANNOTATION_COLORS","ANNOTATION_FONT","MAX_CANVAS_AREA","EXPORT_QUALITY","MIN_PIXEL_BLOCK","PIXEL_BLOCK_DIVISOR","MIN_STROKE","STROKE_DIVISOR","MIN_TEXT_SIZE","TEXT_SIZE_DIVISOR","TEXT_EDGE_DIVISOR","TEXT_LINE_HEIGHT","ARROW_HEAD_LENGTH","ARROW_HEAD_HALF_WIDTH","ARROW_HEAD_SHARE","EXTENSIONS","fitToArea","area","size","maxArea","scale","sizeAfterEdits","edit","edits","width","height","pixelBlock","strokeWidth","textSize","edgeWidth","wrapText","lines","line","word","text","joined","measure","maxWidth","rest","cut","arrowShape","dx","to","from","dy","length","ux","uy","headLength","half","base","dragRect","ax","clamp","bounds","ay","roomX","roomY","aspect","lead","x","y","pixelateData","blockEnd","start","block","by","bottom","bx","right","r","g","b","a","i","alpha","data","count","color","editedFileName","extension","writtenType","originalType","name","dot","loadEditableImage","bitmap","file","original","image","createCanvas","context","renderEdits","canvas","next","rotate","crop","releaseCanvas","drawAnnotation","edge","fill","part","top","at","outline","extra","tracePath","lineEnd","head","exportCanvas","blob","resolve","reject","type","source","turn","rect","cover","style","pixels","first","points","last","point","end","pointIn","max","min","value","_hoisted_1$1","_hoisted_2$1","_hoisted_3","_hoisted_4","_hoisted_5","_hoisted_6","_hoisted_7","_hoisted_8","_hoisted_9","_hoisted_10","_hoisted_11","_hoisted_12","_hoisted_13","MIN_DRAG","MIN_STEP","TEXT_MAX_LENGTH","PkImageEditor_default","defineComponent","\"edits\"","\"editsModifiers\"","__expose","__emit","props","__props","emit","useModel","$t","useI18n","ASPECTS","tool","ref","coverStyle","toolOptions","computed","aspectOptions","coverOptions","colorOptions","annotationHint","aspectLabelId","useId","colorLabelId","redoStack","push","undo","redo","stageEl","useTemplateRef","shown","draw","onMounted","watch","onBeforeUnmount","draftEl","areaEl","room","entry","frameStyle","frameEl","selection","dragFrom","draft","shallowRef","dragPointer","pointOf","box","event","onPointerDown","onPointerMove","stroke","distance","onPointerUp","visible","mark","drawn","onPointerCancel","drawDraft","frame","ratio","k","pendingCrop","applyCrop","selectionStyle","textDraft","textEl","openText","commitText","nextTick","fitText","typed","composing","onTextEnter","onTextEscape","field","pendingText","textStyle","_component_VvIcon","VvIcon","_component_VvButton","VvButtonLocalized","_component_VvButtonGroup","VvButtonGroup","openBlock","createElementBlock","createElementVNode","createVNode","_cache","$event","unref","createSlots","renderList","option","withCtx","toDisplayString","Fragment","normalizeStyle","normalizeClass","createCommentVNode","withDirectives","withModifiers","withKeys","vModelText","_hoisted_1","_hoisted_2","PkImageEditorDialog_default","fullscreen","useMediaQuery","loaded","failed","exporting","editorEl","unmounted","scaledDown","changed","useImage","editor","failedLabel","_component_VvAlert","VvAlert","_component_VvDialog","VvDialog","createBlock"],"sources":["../../../../packages/components/src/chat/imageEdits.ts","../../../../packages/components/src/chat/PkImageEditor.vue","../../../../packages/components/src/chat/PkImageEditor.vue","../../../../packages/components/src/chat/PkImageEditorDialog.vue","../../../../packages/components/src/chat/PkImageEditorDialog.vue"],"sourcesContent":["/**\n * The model of the image editor (spec `chat-image-editor.md`): a list of\n * edits applied in order to the original, so every draw starts again from\n * the original and undo drops the last edit. The geometry is pure; the\n * drawing needs a canvas, which only a browser has.\n */\n\n/** A point in the pixels of the image an edit applies to. */\nexport interface ImagePoint {\n x: number\n y: number\n}\n\nexport interface ImageSize {\n width: number\n height: number\n}\n\n/** A rectangle in the pixels of the image an edit applies to. */\nexport interface ImageRect extends ImagePoint, ImageSize {}\n\n/**\n * How a covered area is drawn: a black box, the only cover a text cannot be\n * read back through, or large blocks of its average color, for a face.\n */\nexport type CoverStyle = 'solid' | 'pixelate'\n\n/** The colors an annotation is drawn in, the same in both themes. */\nexport type AnnotationColor =\n 'red' | 'yellow' | 'green' | 'blue' | 'white' | 'black'\n\n/**\n * Each color with the edge drawn around it, which keeps a mark readable on a\n * background of its own color: a red arrow on a red chart, a white text on a\n * white page.\n */\nexport const ANNOTATION_COLORS: Record<\n AnnotationColor,\n { fill: string; edge: string }\n> = {\n red: { fill: '#ff3b30', edge: '#ffffff' },\n yellow: { fill: '#ffcc00', edge: '#000000' },\n green: { fill: '#34c759', edge: '#000000' },\n blue: { fill: '#007aff', edge: '#ffffff' },\n white: { fill: '#ffffff', edge: '#000000' },\n black: { fill: '#000000', edge: '#ffffff' },\n}\n\n/** The font of a text annotation: the system's, loaded on every page. */\nexport const ANNOTATION_FONT =\n 'system-ui, -apple-system, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif'\n\n/** The tools of the editor, each the kind of edit it adds. */\nexport type EditorTool = 'crop' | 'cover' | 'arrow' | 'rect' | 'path' | 'text'\n\n/**\n * A mark drawn over the image. Its line `width` (the font size of a text) is\n * fixed when it is drawn, so a crop afterwards does not thicken it.\n */\nexport type AnnotationEdit =\n | {\n kind: 'arrow'\n from: ImagePoint\n to: ImagePoint\n color: AnnotationColor\n width: number\n }\n | { kind: 'rect'; rect: ImageRect; color: AnnotationColor; width: number }\n | {\n kind: 'path'\n points: ImagePoint[]\n color: AnnotationColor\n width: number\n }\n | {\n kind: 'text'\n /** The top left corner of the first line. */\n at: ImagePoint\n text: string\n color: AnnotationColor\n size: number\n /** The width its lines wrap in: from `at` to the right edge. */\n maxWidth: number\n }\n\n/**\n * One change, in the coordinates of the image the edits before it left: a\n * crop changes the size, a quarter turn swaps the sides.\n */\nexport type ImageEdit =\n /** A quarter turn, `1` clockwise and `-1` counterclockwise. */\n | { kind: 'rotate'; turn: 1 | -1 }\n | { kind: 'crop'; rect: ImageRect }\n | { kind: 'cover'; rect: ImageRect; style: CoverStyle }\n | AnnotationEdit\n\n/**\n * Safari on iOS refuses a canvas larger than this area: a larger image is\n * scaled down to fit before it is edited.\n */\nexport const MAX_CANVAS_AREA = 4096 * 4096\n\n/** The quality of a JPEG or WebP written back. */\nexport const EXPORT_QUALITY = 0.92\n\n/**\n * The side of a block of a pixelated cover: never under 12 pixels, nor under\n * 1/32 of the short side, so a pixelated cover never turns into a light blur\n * that a face or a word can be guessed through.\n */\nconst MIN_PIXEL_BLOCK = 12\nconst PIXEL_BLOCK_DIVISOR = 32\n\n/**\n * The line of an annotation, and the font size of a text, against the short\n * side of the image: thick enough to read once the chat shows the image\n * small, never under a few pixels on a small one.\n */\nconst MIN_STROKE = 3\nconst STROKE_DIVISOR = 160\nconst MIN_TEXT_SIZE = 16\nconst TEXT_SIZE_DIVISOR = 28\n/** The edge around the letters of a text, against its font size. */\nconst TEXT_EDGE_DIVISOR = 12\n\n/** The lines of a text, in font sizes: the field the text is typed in too. */\nexport const TEXT_LINE_HEIGHT = 1.2\n\n/** The head of an arrow, in line widths: its length, and its half width. */\nconst ARROW_HEAD_LENGTH = 4\nconst ARROW_HEAD_HALF_WIDTH = 2.2\n/** On a short arrow the head takes at most this part of its length. */\nconst ARROW_HEAD_SHARE = 0.6\n\nconst EXTENSIONS: Record<string, string> = {\n 'image/jpeg': 'jpg',\n 'image/png': 'png',\n 'image/webp': 'webp',\n}\n\n/**\n * The size an image is edited at: its own, or the largest with the same\n * proportions whose area fits `maxArea`.\n */\nexport function fitToArea(\n size: ImageSize,\n maxArea = MAX_CANVAS_AREA,\n): ImageSize {\n const area = size.width * size.height\n if (area <= maxArea) {\n return { width: size.width, height: size.height }\n }\n const scale = Math.sqrt(maxArea / area)\n return {\n width: Math.max(1, Math.floor(size.width * scale)),\n height: Math.max(1, Math.floor(size.height * scale)),\n }\n}\n\n/** The size of the image once `edits` are applied to one of `size`. */\nexport function sizeAfterEdits(\n size: ImageSize,\n edits: readonly ImageEdit[],\n): ImageSize {\n let { width, height } = size\n for (const edit of edits) {\n if (edit.kind === 'rotate') {\n ;[width, height] = [height, width]\n } else if (edit.kind === 'crop') {\n ;({ width, height } = edit.rect)\n }\n }\n return { width, height }\n}\n\n/** The side of a block of a pixelated cover on an image of `size`. */\nexport function pixelBlock(size: ImageSize): number {\n return Math.max(\n MIN_PIXEL_BLOCK,\n Math.round(Math.min(size.width, size.height) / PIXEL_BLOCK_DIVISOR),\n )\n}\n\n/** The line width of an arrow, a rectangle or a stroke on an image of `size`. */\nexport function strokeWidth(size: ImageSize): number {\n return Math.max(\n MIN_STROKE,\n Math.round(Math.min(size.width, size.height) / STROKE_DIVISOR),\n )\n}\n\n/** The font size of a text on an image of `size`. */\nexport function textSize(size: ImageSize): number {\n return Math.max(\n MIN_TEXT_SIZE,\n Math.round(Math.min(size.width, size.height) / TEXT_SIZE_DIVISOR),\n )\n}\n\n/** The edge on each side of a line `width` wide. */\nexport function edgeWidth(width: number): number {\n return Math.max(1, Math.round(width / 3))\n}\n\n/**\n * The lines a text takes in `maxWidth`, broken between words as the field\n * it is typed in breaks them, and a word longer than a line where it meets\n * the edge. `measure` gives the width of a string in the text's font.\n */\nexport function wrapText(\n text: string,\n maxWidth: number,\n measure: (part: string) => number,\n): string[] {\n const lines: string[] = []\n let line = ''\n for (const word of text.split(/\\s+/).filter(Boolean)) {\n const joined = line ? `${line} ${word}` : word\n if (measure(joined) <= maxWidth) {\n line = joined\n continue\n }\n if (line) {\n lines.push(line)\n }\n let rest = word\n while (rest.length > 1 && measure(rest) > maxWidth) {\n let cut = rest.length - 1\n while (cut > 1 && measure(rest.slice(0, cut)) > maxWidth) {\n cut--\n }\n lines.push(rest.slice(0, cut))\n rest = rest.slice(cut)\n }\n line = rest\n }\n if (line) {\n lines.push(line)\n }\n return lines\n}\n\n/**\n * An arrow from `from` to its tip `to`: the line stops where the head\n * starts, so its round end does not show past the tip. On a short arrow the\n * head shrinks to stay an arrow and not a triangle.\n */\nexport function arrowShape(\n from: ImagePoint,\n to: ImagePoint,\n width: number,\n): { lineEnd: ImagePoint; head: [ImagePoint, ImagePoint, ImagePoint] } {\n const dx = to.x - from.x\n const dy = to.y - from.y\n const length = Math.hypot(dx, dy)\n if (!length) {\n return { lineEnd: to, head: [to, to, to] }\n }\n const ux = dx / length\n const uy = dy / length\n const headLength = Math.min(\n width * ARROW_HEAD_LENGTH,\n length * ARROW_HEAD_SHARE,\n )\n const half = (headLength * ARROW_HEAD_HALF_WIDTH) / ARROW_HEAD_LENGTH\n const base = { x: to.x - ux * headLength, y: to.y - uy * headLength }\n return {\n lineEnd: base,\n head: [\n { x: to.x, y: to.y },\n { x: base.x - uy * half, y: base.y + ux * half },\n { x: base.x + uy * half, y: base.y - ux * half },\n ],\n }\n}\n\n/**\n * The rectangle a drag from `from` to `to` draws, in any direction, inside\n * `bounds`. With an `aspect` (width over height) the longer side of the drag\n * leads, and both sides shrink together to stay inside the image.\n */\nexport function dragRect(\n from: ImagePoint,\n to: ImagePoint,\n bounds: ImageSize,\n aspect?: number,\n): ImageRect {\n const ax = clamp(from.x, 0, bounds.width)\n const ay = clamp(from.y, 0, bounds.height)\n const dx = to.x - ax\n const dy = to.y - ay\n // The room from the anchor to the edges the drag goes toward\n const roomX = dx < 0 ? ax : bounds.width - ax\n const roomY = dy < 0 ? ay : bounds.height - ay\n let width = Math.min(Math.abs(dx), roomX)\n let height = Math.min(Math.abs(dy), roomY)\n if (aspect) {\n const lead = Math.max(Math.abs(dx), Math.abs(dy) * aspect)\n width = Math.min(lead, roomX, roomY * aspect)\n height = width / aspect\n }\n const x = Math.round(dx < 0 ? ax - width : ax)\n const y = Math.round(dy < 0 ? ay - height : ay)\n return {\n x,\n y,\n width: Math.min(Math.round(width), bounds.width - x),\n height: Math.min(Math.round(height), bounds.height - y),\n }\n}\n\n/**\n * Pixelates RGBA pixels in place: each `block` square takes the average\n * color of the pixels it covers, weighted by their opacity, so the\n * transparent pixels of a PNG do not darken it. The blocks start at the top\n * left corner. What is left on the right and bottom edges, less than a\n * block, joins the last block: a strip of a few pixels would keep a detail\n * finer than the block promises.\n */\nexport function pixelateData(\n data: Uint8ClampedArray,\n width: number,\n height: number,\n block: number,\n): void {\n const blockEnd = (start: number, size: number) =>\n start + 2 * block > size ? size : start + block\n for (let by = 0; by < height; by = blockEnd(by, height)) {\n const bottom = blockEnd(by, height)\n for (let bx = 0; bx < width; bx = blockEnd(bx, width)) {\n const right = blockEnd(bx, width)\n let r = 0\n let g = 0\n let b = 0\n let a = 0\n for (let y = by; y < bottom; y++) {\n for (let x = bx; x < right; x++) {\n const i = (y * width + x) * 4\n const alpha = data[i + 3]\n r += data[i] * alpha\n g += data[i + 1] * alpha\n b += data[i + 2] * alpha\n a += alpha\n }\n }\n const count = (bottom - by) * (right - bx)\n const color =\n a > 0 ? [r / a, g / a, b / a, a / count] : [0, 0, 0, 0]\n for (let y = by; y < bottom; y++) {\n for (let x = bx; x < right; x++) {\n data.set(color, (y * width + x) * 4)\n }\n }\n }\n }\n}\n\n/**\n * The name of the edited file: the original one, with the extension of the\n * type written when the browser could not write the original type (Safari\n * writes no WebP and gives a PNG instead).\n */\nexport function editedFileName(\n name: string,\n originalType: string,\n writtenType: string,\n): string {\n const extension = EXTENSIONS[writtenType]\n if (writtenType === originalType || !extension) {\n return name\n }\n const dot = name.lastIndexOf('.')\n return `${dot > 0 ? name.slice(0, dot) : name}.${extension}`\n}\n\n// --- Canvas ---\n\nexport interface LoadedImage {\n /** The image as it is edited, at most `MAX_CANVAS_AREA` pixels. */\n image: HTMLCanvasElement\n /** The size of the file, larger than `image` when it was scaled down. */\n original: ImageSize\n}\n\n/**\n * Decodes an image file into the canvas the edits start from, upright as\n * the photo's orientation says and scaled down if it is too large for a\n * canvas.\n */\nexport async function loadEditableImage(file: Blob): Promise<LoadedImage> {\n const bitmap = await createImageBitmap(file, {\n imageOrientation: 'from-image',\n })\n try {\n const original = { width: bitmap.width, height: bitmap.height }\n const image = createCanvas(fitToArea(original))\n const g = context(image)\n g.imageSmoothingQuality = 'high'\n g.drawImage(bitmap, 0, 0, image.width, image.height)\n return { image, original }\n } finally {\n bitmap.close()\n }\n}\n\n/**\n * Draws `edits` over a copy of `image`, which stays untouched. The canvas\n * returned is new: the caller releases it with `releaseCanvas` when it\n * draws the next one.\n */\nexport function renderEdits(\n image: HTMLCanvasElement,\n edits: readonly ImageEdit[],\n): HTMLCanvasElement {\n let canvas = createCanvas(image)\n context(canvas).drawImage(image, 0, 0)\n for (const edit of edits) {\n if (edit.kind === 'cover') {\n cover(canvas, edit.rect, edit.style)\n continue\n }\n if (edit.kind !== 'rotate' && edit.kind !== 'crop') {\n drawAnnotation(context(canvas), edit)\n continue\n }\n const next =\n edit.kind === 'rotate'\n ? rotate(canvas, edit.turn)\n : crop(canvas, edit.rect)\n releaseCanvas(canvas)\n canvas = next\n }\n return canvas\n}\n\n/**\n * Draws an annotation in the coordinates `g` is set to: the edited image's\n * on a copy of it, the screen's scaled to the image while it is drawn. The\n * edge goes first, all of it, then the color over it.\n */\nexport function drawAnnotation(\n g: CanvasRenderingContext2D,\n edit: AnnotationEdit,\n): void {\n const { fill, edge } = ANNOTATION_COLORS[edit.color]\n g.save()\n g.lineCap = 'round'\n g.lineJoin = 'round'\n if (edit.kind === 'text') {\n const { at, size } = edit\n g.font = `700 ${size}px ${ANNOTATION_FONT}`\n g.textBaseline = 'top'\n g.lineWidth = Math.max(1, Math.round(size / TEXT_EDGE_DIVISOR)) * 2\n g.strokeStyle = edge\n g.fillStyle = fill\n const lines = wrapText(\n edit.text,\n edit.maxWidth,\n (part) => g.measureText(part).width,\n )\n // Each line sits in the middle of its height, as in the field\n const top = (i: number) =>\n at.y + size * (i * TEXT_LINE_HEIGHT + (TEXT_LINE_HEIGHT - 1) / 2)\n lines.forEach((line, i) => g.strokeText(line, at.x, top(i)))\n lines.forEach((line, i) => g.fillText(line, at.x, top(i)))\n g.restore()\n return\n }\n const outline = edgeWidth(edit.width)\n for (const [color, extra] of [\n [edge, outline * 2],\n [fill, 0],\n ] as const) {\n g.strokeStyle = color\n g.fillStyle = color\n g.lineWidth = edit.width + extra\n if (edit.kind === 'rect') {\n const { x, y, width, height } = edit.rect\n g.strokeRect(x, y, width, height)\n } else if (edit.kind === 'path') {\n tracePath(g, edit.points)\n g.stroke()\n } else {\n const { lineEnd, head } = arrowShape(edit.from, edit.to, edit.width)\n g.beginPath()\n g.moveTo(edit.from.x, edit.from.y)\n g.lineTo(lineEnd.x, lineEnd.y)\n g.stroke()\n g.beginPath()\n g.moveTo(head[0].x, head[0].y)\n g.lineTo(head[1].x, head[1].y)\n g.lineTo(head[2].x, head[2].y)\n g.closePath()\n g.fill()\n if (extra) {\n g.lineWidth = extra\n g.stroke()\n }\n }\n }\n g.restore()\n}\n\n/**\n * Writes the canvas as a file of `type`, or as a PNG where the browser\n * cannot write that type. A canvas writes no metadata: the EXIF of a photo,\n * its position included, does not survive.\n */\nexport function exportCanvas(\n canvas: HTMLCanvasElement,\n type: string,\n): Promise<Blob> {\n return new Promise((resolve, reject) => {\n canvas.toBlob(\n (blob) =>\n blob\n ? resolve(blob)\n : reject(new Error('The image could not be written')),\n type,\n EXPORT_QUALITY,\n )\n })\n}\n\n/**\n * Frees the memory of a canvas no longer shown: Safari counts every canvas\n * until it is collected, and refuses new ones past its total.\n */\nexport function releaseCanvas(canvas: HTMLCanvasElement): void {\n canvas.width = 0\n canvas.height = 0\n}\n\nfunction createCanvas(size: ImageSize): HTMLCanvasElement {\n const canvas = document.createElement('canvas')\n canvas.width = size.width\n canvas.height = size.height\n return canvas\n}\n\nfunction context(canvas: HTMLCanvasElement): CanvasRenderingContext2D {\n const g = canvas.getContext('2d')\n if (!g) {\n throw new Error('No 2D canvas in this browser')\n }\n return g\n}\n\nfunction rotate(source: HTMLCanvasElement, turn: 1 | -1): HTMLCanvasElement {\n const canvas = createCanvas({\n width: source.height,\n height: source.width,\n })\n const g = context(canvas)\n // The context stays the canvas's: the edits after a turn draw through\n // it, and a turn left on it put a solid cover beside what it covers\n g.save()\n g.translate(canvas.width / 2, canvas.height / 2)\n g.rotate((turn * Math.PI) / 2)\n g.drawImage(source, -source.width / 2, -source.height / 2)\n g.restore()\n return canvas\n}\n\nfunction crop(source: HTMLCanvasElement, rect: ImageRect): HTMLCanvasElement {\n const canvas = createCanvas(rect)\n context(canvas).drawImage(\n source,\n rect.x,\n rect.y,\n rect.width,\n rect.height,\n 0,\n 0,\n rect.width,\n rect.height,\n )\n return canvas\n}\n\nfunction cover(\n canvas: HTMLCanvasElement,\n rect: ImageRect,\n style: CoverStyle,\n): void {\n const g = context(canvas)\n if (style === 'solid') {\n g.fillStyle = '#000000'\n g.fillRect(rect.x, rect.y, rect.width, rect.height)\n return\n }\n const pixels = g.getImageData(rect.x, rect.y, rect.width, rect.height)\n pixelateData(pixels.data, rect.width, rect.height, pixelBlock(canvas))\n g.putImageData(pixels, rect.x, rect.y)\n}\n\n/**\n * A stroke through `points`, smoothed: each curve goes through the middle\n * of two points, with the point between them as its control, so a hand\n * drawn line has no corners where the pointer was sampled.\n */\nfunction tracePath(g: CanvasRenderingContext2D, points: ImagePoint[]): void {\n const [first, ...rest] = points\n const last = rest[rest.length - 1]\n g.beginPath()\n if (!first) {\n return\n }\n g.moveTo(first.x, first.y)\n for (let i = 0; i < rest.length - 1; i++) {\n const point = rest[i]\n const next = rest[i + 1]\n g.quadraticCurveTo(\n point.x,\n point.y,\n (point.x + next.x) / 2,\n (point.y + next.y) / 2,\n )\n }\n // A single point is a dot: a line to itself, drawn with its round ends\n const end = last ?? first\n g.lineTo(end.x, end.y)\n}\n\n/** `point`, moved inside an image of `size` when it is past an edge. */\nexport function pointIn(point: ImagePoint, size: ImageSize): ImagePoint {\n return {\n x: clamp(point.x, 0, size.width),\n y: clamp(point.y, 0, size.height),\n }\n}\n\nfunction clamp(value: number, min: number, max: number): number {\n return Math.min(max, Math.max(min, value))\n}\n","<script setup lang=\"ts\">\n import {\n computed,\n nextTick,\n onBeforeUnmount,\n onMounted,\n ref,\n shallowRef,\n useId,\n useTemplateRef,\n watch,\n } from 'vue'\n import { useI18n } from 'vue-i18n'\n import { useResizeObserver } from '@vueuse/core'\n import PkEditorRadioButtons from '../PkEditorRadioButtons.vue'\n import {\n ANNOTATION_COLORS,\n ANNOTATION_FONT,\n dragRect,\n drawAnnotation,\n pointIn,\n releaseCanvas,\n renderEdits,\n sizeAfterEdits,\n strokeWidth,\n TEXT_LINE_HEIGHT,\n textSize,\n type AnnotationColor,\n type AnnotationEdit,\n type CoverStyle,\n type EditorTool,\n type ImageEdit,\n type ImagePoint,\n type ImageRect,\n } from './imageEdits'\n\n /**\n * The tools of the image editor over the image they change (spec\n * `chat-image-editor.md`, F1 and F2): crop and cover by dragging on the\n * image, quarter turns, arrows, rectangles, freehand strokes and texts in\n * a few colors, undo and redo. The edits are the model, the image stays\n * as it was loaded. Drawing needs a mouse, a pen or a finger; the rest\n * works from the keyboard.\n */\n const props = defineProps<{\n /** The image the edits start from. */\n image: HTMLCanvasElement\n /** The tool picked on opening: crop, unless the host says otherwise. */\n initialTool?: EditorTool\n }>()\n\n const emit = defineEmits<{\n /** The browser could not draw the image, out of canvas memory. */\n error: []\n }>()\n\n const edits = defineModel<ImageEdit[]>('edits', { required: true })\n\n const { t: $t } = useI18n({ useScope: 'global' })\n\n // --- Tools ---\n\n type Aspect = 'free' | '1:1' | '4:3' | '16:9'\n\n const ASPECTS: Record<Aspect, number | undefined> = {\n free: undefined,\n '1:1': 1,\n '4:3': 4 / 3,\n '16:9': 16 / 9,\n }\n\n const tool = ref<EditorTool>(props.initialTool ?? 'crop')\n const aspect = ref<Aspect>('free')\n // Every opening starts on the solid box, the only cover safe on a text:\n // the pixels picked for a face yesterday do not cover an IBAN today\n const coverStyle = ref<CoverStyle>('solid')\n const color = ref<AnnotationColor>('red')\n\n const toolOptions = computed<\n { value: EditorTool; label: string; icon: string }[]\n >(() => [\n { value: 'crop', label: $t('imageEditor.crop'), icon: 'ri:crop-line' },\n {\n value: 'cover',\n label: $t('imageEditor.cover'),\n icon: 'ri:eye-off-line',\n },\n {\n value: 'arrow',\n label: $t('imageEditor.arrow'),\n icon: 'ri:arrow-right-up-line',\n },\n {\n value: 'rect',\n label: $t('imageEditor.rect'),\n icon: 'ri:checkbox-blank-line',\n },\n {\n value: 'path',\n label: $t('imageEditor.path'),\n icon: 'ri:pencil-line',\n },\n { value: 'text', label: $t('imageEditor.text'), icon: 'ri:text' },\n ])\n const aspectOptions = computed(() => [\n { value: 'free', label: $t('imageEditor.aspectFree') },\n { value: '1:1', label: '1:1' },\n { value: '4:3', label: '4:3' },\n { value: '16:9', label: '16:9' },\n ])\n const coverOptions = computed(() => [\n { value: 'solid', label: $t('imageEditor.coverSolid') },\n { value: 'pixelate', label: $t('imageEditor.coverPixelate') },\n ])\n const colorOptions = computed<{ value: AnnotationColor; label: string }[]>(\n () => [\n { value: 'red', label: $t('imageEditor.colorRed') },\n { value: 'yellow', label: $t('imageEditor.colorYellow') },\n { value: 'green', label: $t('imageEditor.colorGreen') },\n { value: 'blue', label: $t('imageEditor.colorBlue') },\n { value: 'white', label: $t('imageEditor.colorWhite') },\n { value: 'black', label: $t('imageEditor.colorBlack') },\n ],\n )\n const annotationHint = computed(() => {\n switch (tool.value) {\n case 'arrow':\n return $t('imageEditor.arrowHint')\n case 'rect':\n return $t('imageEditor.rectHint')\n case 'path':\n return $t('imageEditor.pathHint')\n default:\n return $t('imageEditor.textHint')\n }\n })\n const aspectLabelId = useId()\n const colorLabelId = useId()\n\n // --- History ---\n\n const redoStack = ref<ImageEdit[]>([])\n\n function push(edit: ImageEdit) {\n edits.value = [...edits.value, edit]\n redoStack.value = []\n }\n\n function undo() {\n selection.value = undefined\n const last = edits.value[edits.value.length - 1]\n if (!last) {\n return\n }\n edits.value = edits.value.slice(0, -1)\n redoStack.value = [...redoStack.value, last]\n }\n\n function redo() {\n selection.value = undefined\n const next = redoStack.value[redoStack.value.length - 1]\n if (!next) {\n return\n }\n edits.value = [...edits.value, next]\n redoStack.value = redoStack.value.slice(0, -1)\n }\n\n function rotate(turn: 1 | -1) {\n selection.value = undefined\n push({ kind: 'rotate', turn })\n }\n\n // --- Drawing ---\n\n const size = computed(() => sizeAfterEdits(props.image, edits.value))\n const stageEl = useTemplateRef<HTMLDivElement>('stageEl')\n // The canvas on screen, outside Vue: a new one replaces it on each edit\n let shown: HTMLCanvasElement | undefined\n\n function draw() {\n if (!stageEl.value) {\n return\n }\n let canvas: HTMLCanvasElement\n try {\n canvas = renderEdits(props.image, edits.value)\n } catch {\n emit('error')\n return\n }\n canvas.setAttribute('role', 'img')\n canvas.setAttribute('aria-label', $t('imageEditor.canvasLabel'))\n stageEl.value.replaceChildren(canvas)\n if (shown) {\n releaseCanvas(shown)\n }\n shown = canvas\n }\n\n onMounted(draw)\n watch([() => props.image, edits], draw, { flush: 'post' })\n onBeforeUnmount(() => {\n if (shown) {\n releaseCanvas(shown)\n }\n if (draftEl.value) {\n releaseCanvas(draftEl.value)\n }\n })\n\n // The image fits the room it has, never drawn larger than its pixels\n const areaEl = useTemplateRef<HTMLDivElement>('areaEl')\n const room = ref({ width: 0, height: 0 })\n useResizeObserver(areaEl, ([entry]) => {\n if (entry) {\n room.value = {\n width: entry.contentRect.width,\n height: entry.contentRect.height,\n }\n }\n })\n const scale = computed(() =>\n Math.min(\n 1,\n room.value.width / size.value.width,\n room.value.height / size.value.height,\n ),\n )\n const frameStyle = computed(() => ({\n width: `${Math.max(1, Math.floor(size.value.width * scale.value))}px`,\n height: `${Math.max(1, Math.floor(size.value.height * scale.value))}px`,\n }))\n\n // --- Pointer gestures ---\n\n /** A mark shorter than this on screen is a click, not a drag. */\n const MIN_DRAG = 6\n /** A stroke takes a new point once the pointer moved this far on screen. */\n const MIN_STEP = 2\n\n const frameEl = useTemplateRef<HTMLDivElement>('frameEl')\n const selection = ref<ImageRect>()\n const dragFrom = ref<ImagePoint>()\n // The annotation being drawn, over the image until it is let go\n const draft = shallowRef<AnnotationEdit>()\n // The pointer of the gesture: a second finger on the screen is ignored,\n // not a new start that the moves of both would share\n let dragPointer: number | undefined\n\n function pointOf(event: PointerEvent): ImagePoint {\n const box = frameEl.value?.getBoundingClientRect()\n if (!box?.width || !box.height) {\n return { x: 0, y: 0 }\n }\n // A drag goes on past the edge of the image: the mark stops at it\n return pointIn(\n {\n x: ((event.clientX - box.left) / box.width) * size.value.width,\n y: ((event.clientY - box.top) / box.height) * size.value.height,\n },\n size.value,\n )\n }\n\n function onPointerDown(event: PointerEvent) {\n if (event.button !== 0 || dragPointer !== undefined) {\n return\n }\n dragPointer = event.pointerId\n frameEl.value?.setPointerCapture(event.pointerId)\n const point = pointOf(event)\n dragFrom.value = point\n selection.value = undefined\n if (tool.value === 'path') {\n draft.value = {\n kind: 'path',\n points: [point],\n color: color.value,\n width: strokeWidth(size.value),\n }\n }\n }\n\n function onPointerMove(event: PointerEvent) {\n const from = dragFrom.value\n if (!from || event.pointerId !== dragPointer) {\n return\n }\n const to = pointOf(event)\n const width = strokeWidth(size.value)\n switch (tool.value) {\n case 'crop':\n case 'cover':\n selection.value = dragRect(\n from,\n to,\n size.value,\n tool.value === 'crop' ? ASPECTS[aspect.value] : undefined,\n )\n break\n case 'rect':\n draft.value = {\n kind: 'rect',\n rect: dragRect(from, to, size.value),\n color: color.value,\n width,\n }\n break\n case 'arrow':\n draft.value = {\n kind: 'arrow',\n from,\n to,\n color: color.value,\n width,\n }\n break\n case 'path': {\n const stroke = draft.value\n const last =\n stroke?.kind === 'path'\n ? stroke.points[stroke.points.length - 1]\n : undefined\n if (\n stroke?.kind === 'path' &&\n last &&\n distance(last, to) * scale.value >= MIN_STEP\n ) {\n draft.value = { ...stroke, points: [...stroke.points, to] }\n }\n break\n }\n }\n }\n\n function onPointerUp(event: PointerEvent) {\n const from = dragFrom.value\n if (!from || event.pointerId !== dragPointer) {\n return\n }\n dragPointer = undefined\n dragFrom.value = undefined\n if (tool.value === 'crop' || tool.value === 'cover') {\n const rect = selection.value\n if (!rect || !visible(rect.width) || !visible(rect.height)) {\n selection.value = undefined\n return\n }\n // A cover is written as soon as it is drawn; a crop waits for Apply\n if (tool.value === 'cover') {\n selection.value = undefined\n push({ kind: 'cover', rect, style: coverStyle.value })\n }\n return\n }\n if (tool.value === 'text') {\n void openText(from)\n return\n }\n const mark = draft.value\n draft.value = undefined\n if (mark && drawn(mark)) {\n push(mark)\n }\n }\n\n function onPointerCancel(event: PointerEvent) {\n if (event.pointerId !== dragPointer) {\n return\n }\n dragPointer = undefined\n dragFrom.value = undefined\n selection.value = undefined\n draft.value = undefined\n }\n\n /** A length in the image's pixels shows on screen as a drag. */\n function visible(length: number): boolean {\n return length * scale.value >= MIN_DRAG\n }\n\n /** A mark let go is one, not a click that slipped. */\n function drawn(mark: AnnotationEdit): boolean {\n switch (mark.kind) {\n case 'arrow':\n return visible(distance(mark.from, mark.to))\n case 'rect':\n return visible(mark.rect.width) && visible(mark.rect.height)\n case 'path':\n return mark.points.length > 1\n default:\n return true\n }\n }\n\n // The mark being drawn, on a canvas of the frame's size over the image:\n // drawn by the same code as the file, so what shows is what goes. Out of\n // a drag it has no pixels, which Safari would count.\n const draftEl = useTemplateRef<HTMLCanvasElement>('draftEl')\n\n function drawDraft() {\n const canvas = draftEl.value\n const frame = frameEl.value\n if (!canvas || !frame) {\n return\n }\n const mark = draft.value\n if (!mark) {\n releaseCanvas(canvas)\n return\n }\n const ratio = window.devicePixelRatio || 1\n canvas.width = Math.round(frame.clientWidth * ratio)\n canvas.height = Math.round(frame.clientHeight * ratio)\n const g = canvas.getContext('2d')\n if (!g) {\n return\n }\n const k = canvas.width / size.value.width\n g.setTransform(k, 0, 0, k, 0, 0)\n drawAnnotation(g, mark)\n }\n\n watch(draft, drawDraft, { flush: 'post' })\n\n // --- Cropping ---\n\n const pendingCrop = computed(\n () => tool.value === 'crop' && !dragFrom.value && !!selection.value,\n )\n\n function applyCrop() {\n const rect = selection.value\n if (!pendingCrop.value || !rect) {\n return\n }\n selection.value = undefined\n if (\n rect.width === size.value.width &&\n rect.height === size.value.height\n ) {\n return\n }\n push({ kind: 'crop', rect })\n }\n\n // A crop drawn and then given proportions keeps its corner and takes them\n watch(aspect, (value) => {\n const rect = selection.value\n if (rect && tool.value === 'crop') {\n selection.value = dragRect(\n rect,\n { x: rect.x + rect.width, y: rect.y + rect.height },\n size.value,\n ASPECTS[value],\n )\n }\n })\n\n const selectionStyle = computed(() => {\n const rect = selection.value\n if (!rect) {\n return undefined\n }\n return {\n left: `${(rect.x / size.value.width) * 100}%`,\n top: `${(rect.y / size.value.height) * 100}%`,\n width: `${(rect.width / size.value.width) * 100}%`,\n height: `${(rect.height / size.value.height) * 100}%`,\n }\n })\n\n // --- Text ---\n\n // A text being typed, where it starts: it joins the edits on Enter or\n // when the field loses the focus, and goes away on Esc or empty. Its\n // lines wrap in the room up to the right edge, in the field as in the\n // file, where a single line was cut at the edge\n const textDraft = ref<{ at: ImagePoint; text: string }>()\n const textEl = useTemplateRef<HTMLTextAreaElement>('textEl')\n const TEXT_MAX_LENGTH = 200\n\n async function openText(at: ImagePoint) {\n commitText()\n textDraft.value = { at, text: '' }\n await nextTick()\n fitText()\n textEl.value?.focus()\n }\n\n function commitText() {\n const typed = textDraft.value\n textDraft.value = undefined\n const text = typed?.text.trim()\n if (typed && text) {\n push({\n kind: 'text',\n at: typed.at,\n text,\n color: color.value,\n size: textSize(size.value),\n maxWidth: size.value.width - typed.at.x,\n })\n }\n }\n\n /**\n * An input method is composing a word: Enter and Esc are its own, to\n * confirm or drop the word. Safari reports it with `isComposing`\n * already false, hence the 229.\n */\n const composing = (event: KeyboardEvent) =>\n event.isComposing || event.keyCode === 229\n\n function onTextEnter(event: KeyboardEvent) {\n if (!composing(event)) {\n event.preventDefault()\n commitText()\n }\n }\n\n function onTextEscape(event: KeyboardEvent) {\n if (!composing(event)) {\n event.preventDefault()\n textDraft.value = undefined\n }\n }\n\n // The field grows with its lines\n function fitText() {\n const field = textEl.value\n if (field) {\n field.style.height = 'auto'\n field.style.height = `${field.scrollHeight}px`\n }\n }\n\n const pendingText = computed(() => !!textDraft.value?.text.trim())\n\n // The field shows the text as it will be drawn: the color, the font and\n // its size on screen, a shadow standing for the edge\n const textStyle = computed(() => {\n const typed = textDraft.value\n if (!typed) {\n return undefined\n }\n const { fill, edge } = ANNOTATION_COLORS[color.value]\n return {\n left: `${(typed.at.x / size.value.width) * 100}%`,\n top: `${(typed.at.y / size.value.height) * 100}%`,\n fontFamily: ANNOTATION_FONT,\n fontSize: `${textSize(size.value) * scale.value}px`,\n lineHeight: String(TEXT_LINE_HEIGHT),\n color: fill,\n '--pk-image-editor-text-edge': edge,\n }\n })\n\n // A tool picked keeps nothing of the one before: a crop drawn, a mark\n // half drawn; a text typed joins the edits\n watch(tool, () => {\n selection.value = undefined\n draft.value = undefined\n commitText()\n })\n\n defineExpose({\n /** Something is drawn and not added yet: a crop, a text being typed. */\n pending: computed(() => pendingCrop.value || pendingText.value),\n /** Adds it, as Apply and Enter do: \"Use the image\" calls it first. */\n applyPending: () => {\n applyCrop()\n commitText()\n },\n /**\n * The canvas on screen, every edit drawn, which the host writes as\n * the file: it stays the editor's, released when the editor goes.\n */\n shownCanvas: () => shown,\n })\n\n function distance(a: ImagePoint, b: ImagePoint): number {\n return Math.hypot(b.x - a.x, b.y - a.y)\n }\n</script>\n\n<template>\n <div class=\"pk-image-editor\">\n <div class=\"pk-image-editor__toolbar\">\n <PkEditorRadioButtons\n v-model=\"tool\"\n name=\"pk-image-editor-tool\"\n segmented\n class=\"mb-0\"\n :aria-label=\"$t('imageEditor.tool')\"\n :options=\"toolOptions\">\n <template\n v-for=\"option in toolOptions\"\n :key=\"option.value\"\n #[option.value]>\n <span class=\"pk-image-editor__tool\" :title=\"option.label\">\n <VvIcon :name=\"option.icon\" class=\"w-16 h-16\" />\n <span class=\"pk-image-editor__tool-label\">\n {{ option.label }}\n </span>\n </span>\n </template>\n </PkEditorRadioButtons>\n <VvButtonGroup modifiers=\"compact\">\n <VvButton\n modifiers=\"action-quiet\"\n icon=\"ri:anticlockwise-line\"\n :title=\"$t('imageEditor.rotateLeft')\"\n :aria-label=\"$t('imageEditor.rotateLeft')\"\n @click=\"rotate(-1)\" />\n <VvButton\n modifiers=\"action-quiet\"\n icon=\"ri:clockwise-line\"\n :title=\"$t('imageEditor.rotateRight')\"\n :aria-label=\"$t('imageEditor.rotateRight')\"\n @click=\"rotate(1)\" />\n </VvButtonGroup>\n <!-- \"Annulla\" is already the dialog's button: two \"Annulla\"\n doing different things in one window read as one -->\n <VvButtonGroup modifiers=\"compact\" class=\"ml-auto\">\n <VvButton\n modifiers=\"action-quiet\"\n icon=\"ri:arrow-go-back-line\"\n :title=\"$t('imageEditor.undo')\"\n :aria-label=\"$t('imageEditor.undo')\"\n :disabled=\"!edits.length\"\n @click=\"undo\" />\n <VvButton\n modifiers=\"action-quiet\"\n icon=\"ri:arrow-go-forward-line\"\n :title=\"$t('imageEditor.redo')\"\n :aria-label=\"$t('imageEditor.redo')\"\n :disabled=\"!redoStack.length\"\n @click=\"redo\" />\n </VvButtonGroup>\n </div>\n <div class=\"pk-image-editor__options\">\n <template v-if=\"tool === 'crop'\">\n <span :id=\"aspectLabelId\" class=\"pk-image-editor__label\">\n {{ $t('imageEditor.aspect') }}\n </span>\n <PkEditorRadioButtons\n v-model=\"aspect\"\n name=\"pk-image-editor-aspect\"\n segmented\n class=\"mb-0\"\n :aria-labelledby=\"aspectLabelId\"\n :options=\"aspectOptions\" />\n <span class=\"pk-image-editor__hint\">\n {{ $t('imageEditor.cropHint') }}\n </span>\n <VvButton\n class=\"ml-auto\"\n modifiers=\"secondary\"\n :label=\"$t('action.apply')\"\n :disabled=\"!pendingCrop\"\n @click=\"applyCrop\" />\n </template>\n <template v-else-if=\"tool === 'cover'\">\n <PkEditorRadioButtons\n v-model=\"coverStyle\"\n name=\"pk-image-editor-cover\"\n segmented\n class=\"mb-0\"\n :aria-label=\"$t('imageEditor.coverStyle')\"\n :options=\"coverOptions\" />\n <span class=\"pk-image-editor__hint\">\n {{ $t('imageEditor.coverHint') }}\n </span>\n </template>\n <template v-else>\n <span :id=\"colorLabelId\" class=\"pk-image-editor__label\">\n {{ $t('imageEditor.color') }}\n </span>\n <PkEditorRadioButtons\n v-model=\"color\"\n name=\"pk-image-editor-color\"\n segmented\n class=\"mb-0\"\n :aria-labelledby=\"colorLabelId\"\n :options=\"colorOptions\">\n <template\n v-for=\"option in colorOptions\"\n :key=\"option.value\"\n #[option.value]>\n <span\n class=\"pk-image-editor__color\"\n :title=\"option.label\">\n <span\n class=\"pk-image-editor__swatch\"\n :style=\"{\n backgroundColor:\n ANNOTATION_COLORS[option.value].fill,\n }\" />\n <span class=\"u-visually-hidden\">\n {{ option.label }}\n </span>\n </span>\n </template>\n </PkEditorRadioButtons>\n <span class=\"pk-image-editor__hint\">\n {{ annotationHint }}\n </span>\n </template>\n </div>\n <div ref=\"areaEl\" class=\"pk-image-editor__area\">\n <div\n ref=\"frameEl\"\n class=\"pk-image-editor__frame\"\n :class=\"{ 'pk-image-editor__frame--text': tool === 'text' }\"\n :style=\"frameStyle\"\n @pointerdown=\"onPointerDown\"\n @pointermove=\"onPointerMove\"\n @pointerup=\"onPointerUp\"\n @pointercancel=\"onPointerCancel\">\n <div ref=\"stageEl\" class=\"pk-image-editor__stage\" />\n <canvas\n ref=\"draftEl\"\n class=\"pk-image-editor__draft\"\n width=\"0\"\n height=\"0\"\n aria-hidden=\"true\" />\n <div\n v-if=\"selection\"\n class=\"pk-image-editor__selection\"\n :class=\"`pk-image-editor__selection--${tool}`\"\n :style=\"selectionStyle\" />\n <textarea\n v-if=\"textDraft\"\n ref=\"textEl\"\n v-model=\"textDraft.text\"\n class=\"pk-image-editor__text\"\n rows=\"1\"\n :maxlength=\"TEXT_MAX_LENGTH\"\n :aria-label=\"$t('imageEditor.textLabel')\"\n :style=\"textStyle\"\n @pointerdown.stop\n @input=\"fitText\"\n @keydown.enter=\"onTextEnter\"\n @keydown.esc=\"onTextEscape\"\n @blur=\"commitText\" />\n </div>\n </div>\n </div>\n</template>\n\n<style lang=\"scss\">\n .pk-image-editor {\n display: flex;\n flex: 1;\n flex-direction: column;\n min-height: 0;\n\n // The tool names hide when the dialog is too narrow for them: the\n // icons stay, named by their title and for a screen reader\n container: pk-image-editor / inline-size;\n\n &__toolbar,\n &__options {\n display: flex;\n flex-wrap: wrap;\n align-items: center;\n gap: var(--spacing-8);\n padding: var(--spacing-8) var(--spacing-md);\n border-bottom: 1px solid var(--color-surface-3);\n }\n\n &__toolbar {\n background-color: var(--color-surface-1);\n }\n\n // A segmented control is as wide as its options, each as wide as its\n // words: with the library's equal segments the longer one was cut\n // (\"Riquadro pien\") and its icon shrank. A row too narrow sends the\n // control to a line of its own.\n &__toolbar > .vv-radio-group,\n &__options > .vv-radio-group {\n flex: none;\n width: auto;\n\n .vv-radio {\n flex: none;\n }\n }\n\n &__tool {\n display: inline-flex;\n align-items: center;\n gap: var(--spacing-4);\n }\n\n &__label {\n color: var(--color-word-2);\n font-size: var(--text-14);\n }\n\n &__hint {\n color: var(--color-word-3);\n font-size: var(--text-12);\n }\n\n // Positioned, for the name a screen reader reads in it\n &__color {\n position: relative;\n display: inline-flex;\n }\n\n // A dot of the color, with a hairline that keeps the white one on a\n // light surface and the black one on a dark surface\n &__swatch {\n display: block;\n width: var(--spacing-16);\n height: var(--spacing-16);\n border-radius: 50%;\n box-shadow: inset 0 0 0 1px\n color-mix(in srgb, var(--color-word) 40%, transparent);\n }\n\n // A checkerboard, as in the lightbox: a transparent PNG shows where\n // it is transparent\n &__area {\n display: flex;\n flex: 1;\n align-items: center;\n justify-content: center;\n min-height: 12rem;\n padding: var(--spacing-md);\n overflow: hidden;\n background: repeating-conic-gradient(\n var(--color-surface-2) 0% 25%,\n var(--color-surface) 0% 50%\n )\n 50% / 16px 16px;\n }\n\n &__frame {\n position: relative;\n flex-shrink: 0;\n overflow: hidden;\n box-shadow: var(--shadow-sm);\n cursor: crosshair;\n\n // The drag is the gesture: no scroll or zoom of the page under it\n touch-action: none;\n user-select: none;\n\n &--text {\n cursor: text;\n }\n }\n\n &__stage,\n &__stage > canvas {\n display: block;\n width: 100%;\n height: 100%;\n }\n\n &__draft {\n position: absolute;\n inset: 0;\n width: 100%;\n height: 100%;\n pointer-events: none;\n }\n\n // Drawn over a photo, so the same in both themes: white with a dark\n // edge reads on any image\n &__selection {\n position: absolute;\n pointer-events: none;\n border: 2px solid var(--color-white);\n outline: 1px solid\n color-mix(in srgb, var(--color-black) 50%, transparent);\n\n // What a crop leaves out goes dark\n &--crop {\n box-shadow: 0 0 0 100vmax\n color-mix(in srgb, var(--color-black) 55%, transparent);\n }\n\n &--cover {\n border-style: dashed;\n background-color: color-mix(\n in srgb,\n var(--color-black) 30%,\n transparent\n );\n }\n }\n\n // The text being typed, where it will be drawn: from the point\n // clicked to the right edge of the image\n &__text {\n position: absolute;\n right: 0;\n display: block;\n margin: 0;\n padding: 0;\n overflow: hidden;\n border: 0;\n outline: 1px dashed\n color-mix(in srgb, var(--color-white) 80%, transparent);\n background: transparent;\n font-weight: 700;\n white-space: pre-wrap;\n overflow-wrap: anywhere;\n resize: none;\n caret-color: currentcolor;\n user-select: text;\n text-shadow:\n 0 0 1px var(--pk-image-editor-text-edge),\n 0 0 2px var(--pk-image-editor-text-edge),\n 0 0 3px var(--pk-image-editor-text-edge);\n }\n }\n\n // A finger gets 40px (DESIGN.md): a segment is drawn 32px tall, and its\n // area grows around it, as `u-touch-target` does\n @media (pointer: coarse) {\n .pk-image-editor .vv-radio--segment::after {\n content: '';\n position: absolute;\n top: 50%;\n left: 50%;\n width: max(100%, var(--spacing-40));\n height: max(100%, var(--spacing-40));\n transform: translate(-50%, -50%);\n }\n }\n\n // Same as `u-visually-hidden`, which a class cannot get from the width\n // of a container. Under 50rem the named tools pushed undo and redo to a\n // row of their own (760px, 2026-11-04)\n @container pk-image-editor (max-width: 50rem) {\n .pk-image-editor__tool-label {\n position: absolute;\n width: 1px;\n height: 1px;\n overflow: hidden;\n clip-path: inset(50%);\n white-space: nowrap;\n }\n }\n</style>\n","<script setup lang=\"ts\">\n import {\n computed,\n nextTick,\n onBeforeUnmount,\n onMounted,\n ref,\n shallowRef,\n useId,\n useTemplateRef,\n watch,\n } from 'vue'\n import { useI18n } from 'vue-i18n'\n import { useResizeObserver } from '@vueuse/core'\n import PkEditorRadioButtons from '../PkEditorRadioButtons.vue'\n import {\n ANNOTATION_COLORS,\n ANNOTATION_FONT,\n dragRect,\n drawAnnotation,\n pointIn,\n releaseCanvas,\n renderEdits,\n sizeAfterEdits,\n strokeWidth,\n TEXT_LINE_HEIGHT,\n textSize,\n type AnnotationColor,\n type AnnotationEdit,\n type CoverStyle,\n type EditorTool,\n type ImageEdit,\n type ImagePoint,\n type ImageRect,\n } from './imageEdits'\n\n /**\n * The tools of the image editor over the image they change (spec\n * `chat-image-editor.md`, F1 and F2): crop and cover by dragging on the\n * image, quarter turns, arrows, rectangles, freehand strokes and texts in\n * a few colors, undo and redo. The edits are the model, the image stays\n * as it was loaded. Drawing needs a mouse, a pen or a finger; the rest\n * works from the keyboard.\n */\n const props = defineProps<{\n /** The image the edits start from. */\n image: HTMLCanvasElement\n /** The tool picked on opening: crop, unless the host says otherwise. */\n initialTool?: EditorTool\n }>()\n\n const emit = defineEmits<{\n /** The browser could not draw the image, out of canvas memory. */\n error: []\n }>()\n\n const edits = defineModel<ImageEdit[]>('edits', { required: true })\n\n const { t: $t } = useI18n({ useScope: 'global' })\n\n // --- Tools ---\n\n type Aspect = 'free' | '1:1' | '4:3' | '16:9'\n\n const ASPECTS: Record<Aspect, number | undefined> = {\n free: undefined,\n '1:1': 1,\n '4:3': 4 / 3,\n '16:9': 16 / 9,\n }\n\n const tool = ref<EditorTool>(props.initialTool ?? 'crop')\n const aspect = ref<Aspect>('free')\n // Every opening starts on the solid box, the only cover safe on a text:\n // the pixels picked for a face yesterday do not cover an IBAN today\n const coverStyle = ref<CoverStyle>('solid')\n const color = ref<AnnotationColor>('red')\n\n const toolOptions = computed<\n { value: EditorTool; label: string; icon: string }[]\n >(() => [\n { value: 'crop', label: $t('imageEditor.crop'), icon: 'ri:crop-line' },\n {\n value: 'cover',\n label: $t('imageEditor.cover'),\n icon: 'ri:eye-off-line',\n },\n {\n value: 'arrow',\n label: $t('imageEditor.arrow'),\n icon: 'ri:arrow-right-up-line',\n },\n {\n value: 'rect',\n label: $t('imageEditor.rect'),\n icon: 'ri:checkbox-blank-line',\n },\n {\n value: 'path',\n label: $t('imageEditor.path'),\n icon: 'ri:pencil-line',\n },\n { value: 'text', label: $t('imageEditor.text'), icon: 'ri:text' },\n ])\n const aspectOptions = computed(() => [\n { value: 'free', label: $t('imageEditor.aspectFree') },\n { value: '1:1', label: '1:1' },\n { value: '4:3', label: '4:3' },\n { value: '16:9', label: '16:9' },\n ])\n const coverOptions = computed(() => [\n { value: 'solid', label: $t('imageEditor.coverSolid') },\n { value: 'pixelate', label: $t('imageEditor.coverPixelate') },\n ])\n const colorOptions = computed<{ value: AnnotationColor; label: string }[]>(\n () => [\n { value: 'red', label: $t('imageEditor.colorRed') },\n { value: 'yellow', label: $t('imageEditor.colorYellow') },\n { value: 'green', label: $t('imageEditor.colorGreen') },\n { value: 'blue', label: $t('imageEditor.colorBlue') },\n { value: 'white', label: $t('imageEditor.colorWhite') },\n { value: 'black', label: $t('imageEditor.colorBlack') },\n ],\n )\n const annotationHint = computed(() => {\n switch (tool.value) {\n case 'arrow':\n return $t('imageEditor.arrowHint')\n case 'rect':\n return $t('imageEditor.rectHint')\n case 'path':\n return $t('imageEditor.pathHint')\n default:\n return $t('imageEditor.textHint')\n }\n })\n const aspectLabelId = useId()\n const colorLabelId = useId()\n\n // --- History ---\n\n const redoStack = ref<ImageEdit[]>([])\n\n function push(edit: ImageEdit) {\n edits.value = [...edits.value, edit]\n redoStack.value = []\n }\n\n function undo() {\n selection.value = undefined\n const last = edits.value[edits.value.length - 1]\n if (!last) {\n return\n }\n edits.value = edits.value.slice(0, -1)\n redoStack.value = [...redoStack.value, last]\n }\n\n function redo() {\n selection.value = undefined\n const next = redoStack.value[redoStack.value.length - 1]\n if (!next) {\n return\n }\n edits.value = [...edits.value, next]\n redoStack.value = redoStack.value.slice(0, -1)\n }\n\n function rotate(turn: 1 | -1) {\n selection.value = undefined\n push({ kind: 'rotate', turn })\n }\n\n // --- Drawing ---\n\n const size = computed(() => sizeAfterEdits(props.image, edits.value))\n const stageEl = useTemplateRef<HTMLDivElement>('stageEl')\n // The canvas on screen, outside Vue: a new one replaces it on each edit\n let shown: HTMLCanvasElement | undefined\n\n function draw() {\n if (!stageEl.value) {\n return\n }\n let canvas: HTMLCanvasElement\n try {\n canvas = renderEdits(props.image, edits.value)\n } catch {\n emit('error')\n return\n }\n canvas.setAttribute('role', 'img')\n canvas.setAttribute('aria-label', $t('imageEditor.canvasLabel'))\n stageEl.value.replaceChildren(canvas)\n if (shown) {\n releaseCanvas(shown)\n }\n shown = canvas\n }\n\n onMounted(draw)\n watch([() => props.image, edits], draw, { flush: 'post' })\n onBeforeUnmount(() => {\n if (shown) {\n releaseCanvas(shown)\n }\n if (draftEl.value) {\n releaseCanvas(draftEl.value)\n }\n })\n\n // The image fits the room it has, never drawn larger than its pixels\n const areaEl = useTemplateRef<HTMLDivElement>('areaEl')\n const room = ref({ width: 0, height: 0 })\n useResizeObserver(areaEl, ([entry]) => {\n if (entry) {\n room.value = {\n width: entry.contentRect.width,\n height: entry.contentRect.height,\n }\n }\n })\n const scale = computed(() =>\n Math.min(\n 1,\n room.value.width / size.value.width,\n room.value.height / size.value.height,\n ),\n )\n const frameStyle = computed(() => ({\n width: `${Math.max(1, Math.floor(size.value.width * scale.value))}px`,\n height: `${Math.max(1, Math.floor(size.value.height * scale.value))}px`,\n }))\n\n // --- Pointer gestures ---\n\n /** A mark shorter than this on screen is a click, not a drag. */\n const MIN_DRAG = 6\n /** A stroke takes a new point once the pointer moved this far on screen. */\n const MIN_STEP = 2\n\n const frameEl = useTemplateRef<HTMLDivElement>('frameEl')\n const selection = ref<ImageRect>()\n const dragFrom = ref<ImagePoint>()\n // The annotation being drawn, over the image until it is let go\n const draft = shallowRef<AnnotationEdit>()\n // The pointer of the gesture: a second finger on the screen is ignored,\n // not a new start that the moves of both would share\n let dragPointer: number | undefined\n\n function pointOf(event: PointerEvent): ImagePoint {\n const box = frameEl.value?.getBoundingClientRect()\n if (!box?.width || !box.height) {\n return { x: 0, y: 0 }\n }\n // A drag goes on past the edge of the image: the mark stops at it\n return pointIn(\n {\n x: ((event.clientX - box.left) / box.width) * size.value.width,\n y: ((event.clientY - box.top) / box.height) * size.value.height,\n },\n size.value,\n )\n }\n\n function onPointerDown(event: PointerEvent) {\n if (event.button !== 0 || dragPointer !== undefined) {\n return\n }\n dragPointer = event.pointerId\n frameEl.value?.setPointerCapture(event.pointerId)\n const point = pointOf(event)\n dragFrom.value = point\n selection.value = undefined\n if (tool.value === 'path') {\n draft.value = {\n kind: 'path',\n points: [point],\n color: color.value,\n width: strokeWidth(size.value),\n }\n }\n }\n\n function onPointerMove(event: PointerEvent) {\n const from = dragFrom.value\n if (!from || event.pointerId !== dragPointer) {\n return\n }\n const to = pointOf(event)\n const width = strokeWidth(size.value)\n switch (tool.value) {\n case 'crop':\n case 'cover':\n selection.value = dragRect(\n from,\n to,\n size.value,\n tool.value === 'crop' ? ASPECTS[aspect.value] : undefined,\n )\n break\n case 'rect':\n draft.value = {\n kind: 'rect',\n rect: dragRect(from, to, size.value),\n color: color.value,\n width,\n }\n break\n case 'arrow':\n draft.value = {\n kind: 'arrow',\n from,\n to,\n color: color.value,\n width,\n }\n break\n case 'path': {\n const stroke = draft.value\n const last =\n stroke?.kind === 'path'\n ? stroke.points[stroke.points.length - 1]\n : undefined\n if (\n stroke?.kind === 'path' &&\n last &&\n distance(last, to) * scale.value >= MIN_STEP\n ) {\n draft.value = { ...stroke, points: [...stroke.points, to] }\n }\n break\n }\n }\n }\n\n function onPointerUp(event: PointerEvent) {\n const from = dragFrom.value\n if (!from || event.pointerId !== dragPointer) {\n return\n }\n dragPointer = undefined\n dragFrom.value = undefined\n if (tool.value === 'crop' || tool.value === 'cover') {\n const rect = selection.value\n if (!rect || !visible(rect.width) || !visible(rect.height)) {\n selection.value = undefined\n return\n }\n // A cover is written as soon as it is drawn; a crop waits for Apply\n if (tool.value === 'cover') {\n selection.value = undefined\n push({ kind: 'cover', rect, style: coverStyle.value })\n }\n return\n }\n if (tool.value === 'text') {\n void openText(from)\n return\n }\n const mark = draft.value\n draft.value = undefined\n if (mark && drawn(mark)) {\n push(mark)\n }\n }\n\n function onPointerCancel(event: PointerEvent) {\n if (event.pointerId !== dragPointer) {\n return\n }\n dragPointer = undefined\n dragFrom.value = undefined\n selection.value = undefined\n draft.value = undefined\n }\n\n /** A length in the image's pixels shows on screen as a drag. */\n function visible(length: number): boolean {\n return length * scale.value >= MIN_DRAG\n }\n\n /** A mark let go is one, not a click that slipped. */\n function drawn(mark: AnnotationEdit): boolean {\n switch (mark.kind) {\n case 'arrow':\n return visible(distance(mark.from, mark.to))\n case 'rect':\n return visible(mark.rect.width) && visible(mark.rect.height)\n case 'path':\n return mark.points.length > 1\n default:\n return true\n }\n }\n\n // The mark being drawn, on a canvas of the frame's size over the image:\n // drawn by the same code as the file, so what shows is what goes. Out of\n // a drag it has no pixels, which Safari would count.\n const draftEl = useTemplateRef<HTMLCanvasElement>('draftEl')\n\n function drawDraft() {\n const canvas = draftEl.value\n const frame = frameEl.value\n if (!canvas || !frame) {\n return\n }\n const mark = draft.value\n if (!mark) {\n releaseCanvas(canvas)\n return\n }\n const ratio = window.devicePixelRatio || 1\n canvas.width = Math.round(frame.clientWidth * ratio)\n canvas.height = Math.round(frame.clientHeight * ratio)\n const g = canvas.getContext('2d')\n if (!g) {\n return\n }\n const k = canvas.width / size.value.width\n g.setTransform(k, 0, 0, k, 0, 0)\n drawAnnotation(g, mark)\n }\n\n watch(draft, drawDraft, { flush: 'post' })\n\n // --- Cropping ---\n\n const pendingCrop = computed(\n () => tool.value === 'crop' && !dragFrom.value && !!selection.value,\n )\n\n function applyCrop() {\n const rect = selection.value\n if (!pendingCrop.value || !rect) {\n return\n }\n selection.value = undefined\n if (\n rect.width === size.value.width &&\n rect.height === size.value.height\n ) {\n return\n }\n push({ kind: 'crop', rect })\n }\n\n // A crop drawn and then given proportions keeps its corner and takes them\n watch(aspect, (value) => {\n const rect = selection.value\n if (rect && tool.value === 'crop') {\n selection.value = dragRect(\n rect,\n { x: rect.x + rect.width, y: rect.y + rect.height },\n size.value,\n ASPECTS[value],\n )\n }\n })\n\n const selectionStyle = computed(() => {\n const rect = selection.value\n if (!rect) {\n return undefined\n }\n return {\n left: `${(rect.x / size.value.width) * 100}%`,\n top: `${(rect.y / size.value.height) * 100}%`,\n width: `${(rect.width / size.value.width) * 100}%`,\n height: `${(rect.height / size.value.height) * 100}%`,\n }\n })\n\n // --- Text ---\n\n // A text being typed, where it starts: it joins the edits on Enter or\n // when the field loses the focus, and goes away on Esc or empty. Its\n // lines wrap in the room up to the right edge, in the field as in the\n // file, where a single line was cut at the edge\n const textDraft = ref<{ at: ImagePoint; text: string }>()\n const textEl = useTemplateRef<HTMLTextAreaElement>('textEl')\n const TEXT_MAX_LENGTH = 200\n\n async function openText(at: ImagePoint) {\n commitText()\n textDraft.value = { at, text: '' }\n await nextTick()\n fitText()\n textEl.value?.focus()\n }\n\n function commitText() {\n const typed = textDraft.value\n textDraft.value = undefined\n const text = typed?.text.trim()\n if (typed && text) {\n push({\n kind: 'text',\n at: typed.at,\n text,\n color: color.value,\n size: textSize(size.value),\n maxWidth: size.value.width - typed.at.x,\n })\n }\n }\n\n /**\n * An input method is composing a word: Enter and Esc are its own, to\n * confirm or drop the word. Safari reports it with `isComposing`\n * already false, hence the 229.\n */\n const composing = (event: KeyboardEvent) =>\n event.isComposing || event.keyCode === 229\n\n function onTextEnter(event: KeyboardEvent) {\n if (!composing(event)) {\n event.preventDefault()\n commitText()\n }\n }\n\n function onTextEscape(event: KeyboardEvent) {\n if (!composing(event)) {\n event.preventDefault()\n textDraft.value = undefined\n }\n }\n\n // The field grows with its lines\n function fitText() {\n const field = textEl.value\n if (field) {\n field.style.height = 'auto'\n field.style.height = `${field.scrollHeight}px`\n }\n }\n\n const pendingText = computed(() => !!textDraft.value?.text.trim())\n\n // The field shows the text as it will be drawn: the color, the font and\n // its size on screen, a shadow standing for the edge\n const textStyle = computed(() => {\n const typed = textDraft.value\n if (!typed) {\n return undefined\n }\n const { fill, edge } = ANNOTATION_COLORS[color.value]\n return {\n left: `${(typed.at.x / size.value.width) * 100}%`,\n top: `${(typed.at.y / size.value.height) * 100}%`,\n fontFamily: ANNOTATION_FONT,\n fontSize: `${textSize(size.value) * scale.value}px`,\n lineHeight: String(TEXT_LINE_HEIGHT),\n color: fill,\n '--pk-image-editor-text-edge': edge,\n }\n })\n\n // A tool picked keeps nothing of the one before: a crop drawn, a mark\n // half drawn; a text typed joins the edits\n watch(tool, () => {\n selection.value = undefined\n draft.value = undefined\n commitText()\n })\n\n defineExpose({\n /** Something is drawn and not added yet: a crop, a text being typed. */\n pending: computed(() => pendingCrop.value || pendingText.value),\n /** Adds it, as Apply and Enter do: \"Use the image\" calls it first. */\n applyPending: () => {\n applyCrop()\n commitText()\n },\n /**\n * The canvas on screen, every edit drawn, which the host writes as\n * the file: it stays the editor's, released when the editor goes.\n */\n shownCanvas: () => shown,\n })\n\n function distance(a: ImagePoint, b: ImagePoint): number {\n return Math.hypot(b.x - a.x, b.y - a.y)\n }\n</script>\n\n<template>\n <div class=\"pk-image-editor\">\n <div class=\"pk-image-editor__toolbar\">\n <PkEditorRadioButtons\n v-model=\"tool\"\n name=\"pk-image-editor-tool\"\n segmented\n class=\"mb-0\"\n :aria-label=\"$t('imageEditor.tool')\"\n :options=\"toolOptions\">\n <template\n v-for=\"option in toolOptions\"\n :key=\"option.value\"\n #[option.value]>\n <span class=\"pk-image-editor__tool\" :title=\"option.label\">\n <VvIcon :name=\"option.icon\" class=\"w-16 h-16\" />\n <span class=\"pk-image-editor__tool-label\">\n {{ option.label }}\n </span>\n </span>\n </template>\n </PkEditorRadioButtons>\n <VvButtonGroup modifiers=\"compact\">\n <VvButton\n modifiers=\"action-quiet\"\n icon=\"ri:anticlockwise-line\"\n :title=\"$t('imageEditor.rotateLeft')\"\n :aria-label=\"$t('imageEditor.rotateLeft')\"\n @click=\"rotate(-1)\" />\n <VvButton\n modifiers=\"action-quiet\"\n icon=\"ri:clockwise-line\"\n :title=\"$t('imageEditor.rotateRight')\"\n :aria-label=\"$t('imageEditor.rotateRight')\"\n @click=\"rotate(1)\" />\n </VvButtonGroup>\n <!-- \"Annulla\" is already the dialog's button: two \"Annulla\"\n doing different things in one window read as one -->\n <VvButtonGroup modifiers=\"compact\" class=\"ml-auto\">\n <VvButton\n modifiers=\"action-quiet\"\n icon=\"ri:arrow-go-back-line\"\n :title=\"$t('imageEditor.undo')\"\n :aria-label=\"$t('imageEditor.undo')\"\n :disabled=\"!edits.length\"\n @click=\"undo\" />\n <VvButton\n modifiers=\"action-quiet\"\n icon=\"ri:arrow-go-forward-line\"\n :title=\"$t('imageEditor.redo')\"\n :aria-label=\"$t('imageEditor.redo')\"\n :disabled=\"!redoStack.length\"\n @click=\"redo\" />\n </VvButtonGroup>\n </div>\n <div class=\"pk-image-editor__options\">\n <template v-if=\"tool === 'crop'\">\n <span :id=\"aspectLabelId\" class=\"pk-image-editor__label\">\n {{ $t('imageEditor.aspect') }}\n </span>\n <PkEditorRadioButtons\n v-model=\"aspect\"\n name=\"pk-image-editor-aspect\"\n segmented\n class=\"mb-0\"\n :aria-labelledby=\"aspectLabelId\"\n :options=\"aspectOptions\" />\n <span class=\"pk-image-editor__hint\">\n {{ $t('imageEditor.cropHint') }}\n </span>\n <VvButton\n class=\"ml-auto\"\n modifiers=\"secondary\"\n :label=\"$t('action.apply')\"\n :disabled=\"!pendingCrop\"\n @click=\"applyCrop\" />\n </template>\n <template v-else-if=\"tool === 'cover'\">\n <PkEditorRadioButtons\n v-model=\"coverStyle\"\n name=\"pk-image-editor-cover\"\n segmented\n class=\"mb-0\"\n :aria-label=\"$t('imageEditor.coverStyle')\"\n :options=\"coverOptions\" />\n <span class=\"pk-image-editor__hint\">\n {{ $t('imageEditor.coverHint') }}\n </span>\n </template>\n <template v-else>\n <span :id=\"colorLabelId\" class=\"pk-image-editor__label\">\n {{ $t('imageEditor.color') }}\n </span>\n <PkEditorRadioButtons\n v-model=\"color\"\n name=\"pk-image-editor-color\"\n segmented\n class=\"mb-0\"\n :aria-labelledby=\"colorLabelId\"\n :options=\"colorOptions\">\n <template\n v-for=\"option in colorOptions\"\n :key=\"option.value\"\n #[option.value]>\n <span\n class=\"pk-image-editor__color\"\n :title=\"option.label\">\n <span\n class=\"pk-image-editor__swatch\"\n :style=\"{\n backgroundColor:\n ANNOTATION_COLORS[option.value].fill,\n }\" />\n <span class=\"u-visually-hidden\">\n {{ option.label }}\n </span>\n </span>\n </template>\n </PkEditorRadioButtons>\n <span class=\"pk-image-editor__hint\">\n {{ annotationHint }}\n </span>\n </template>\n </div>\n <div ref=\"areaEl\" class=\"pk-image-editor__area\">\n <div\n ref=\"frameEl\"\n class=\"pk-image-editor__frame\"\n :class=\"{ 'pk-image-editor__frame--text': tool === 'text' }\"\n :style=\"frameStyle\"\n @pointerdown=\"onPointerDown\"\n @pointermove=\"onPointerMove\"\n @pointerup=\"onPointerUp\"\n @pointercancel=\"onPointerCancel\">\n <div ref=\"stageEl\" class=\"pk-image-editor__stage\" />\n <canvas\n ref=\"draftEl\"\n class=\"pk-image-editor__draft\"\n width=\"0\"\n height=\"0\"\n aria-hidden=\"true\" />\n <div\n v-if=\"selection\"\n class=\"pk-image-editor__selection\"\n :class=\"`pk-image-editor__selection--${tool}`\"\n :style=\"selectionStyle\" />\n <textarea\n v-if=\"textDraft\"\n ref=\"textEl\"\n v-model=\"textDraft.text\"\n class=\"pk-image-editor__text\"\n rows=\"1\"\n :maxlength=\"TEXT_MAX_LENGTH\"\n :aria-label=\"$t('imageEditor.textLabel')\"\n :style=\"textStyle\"\n @pointerdown.stop\n @input=\"fitText\"\n @keydown.enter=\"onTextEnter\"\n @keydown.esc=\"onTextEscape\"\n @blur=\"commitText\" />\n </div>\n </div>\n </div>\n</template>\n\n<style lang=\"scss\">\n .pk-image-editor {\n display: flex;\n flex: 1;\n flex-direction: column;\n min-height: 0;\n\n // The tool names hide when the dialog is too narrow for them: the\n // icons stay, named by their title and for a screen reader\n container: pk-image-editor / inline-size;\n\n &__toolbar,\n &__options {\n display: flex;\n flex-wrap: wrap;\n align-items: center;\n gap: var(--spacing-8);\n padding: var(--spacing-8) var(--spacing-md);\n border-bottom: 1px solid var(--color-surface-3);\n }\n\n &__toolbar {\n background-color: var(--color-surface-1);\n }\n\n // A segmented control is as wide as its options, each as wide as its\n // words: with the library's equal segments the longer one was cut\n // (\"Riquadro pien\") and its icon shrank. A row too narrow sends the\n // control to a line of its own.\n &__toolbar > .vv-radio-group,\n &__options > .vv-radio-group {\n flex: none;\n width: auto;\n\n .vv-radio {\n flex: none;\n }\n }\n\n &__tool {\n display: inline-flex;\n align-items: center;\n gap: var(--spacing-4);\n }\n\n &__label {\n color: var(--color-word-2);\n font-size: var(--text-14);\n }\n\n &__hint {\n color: var(--color-word-3);\n font-size: var(--text-12);\n }\n\n // Positioned, for the name a screen reader reads in it\n &__color {\n position: relative;\n display: inline-flex;\n }\n\n // A dot of the color, with a hairline that keeps the white one on a\n // light surface and the black one on a dark surface\n &__swatch {\n display: block;\n width: var(--spacing-16);\n height: var(--spacing-16);\n border-radius: 50%;\n box-shadow: inset 0 0 0 1px\n color-mix(in srgb, var(--color-word) 40%, transparent);\n }\n\n // A checkerboard, as in the lightbox: a transparent PNG shows where\n // it is transparent\n &__area {\n display: flex;\n flex: 1;\n align-items: center;\n justify-content: center;\n min-height: 12rem;\n padding: var(--spacing-md);\n overflow: hidden;\n background: repeating-conic-gradient(\n var(--color-surface-2) 0% 25%,\n var(--color-surface) 0% 50%\n )\n 50% / 16px 16px;\n }\n\n &__frame {\n position: relative;\n flex-shrink: 0;\n overflow: hidden;\n box-shadow: var(--shadow-sm);\n cursor: crosshair;\n\n // The drag is the gesture: no scroll or zoom of the page under it\n touch-action: none;\n user-select: none;\n\n &--text {\n cursor: text;\n }\n }\n\n &__stage,\n &__stage > canvas {\n display: block;\n width: 100%;\n height: 100%;\n }\n\n &__draft {\n position: absolute;\n inset: 0;\n width: 100%;\n height: 100%;\n pointer-events: none;\n }\n\n // Drawn over a photo, so the same in both themes: white with a dark\n // edge reads on any image\n &__selection {\n position: absolute;\n pointer-events: none;\n border: 2px solid var(--color-white);\n outline: 1px solid\n color-mix(in srgb, var(--color-black) 50%, transparent);\n\n // What a crop leaves out goes dark\n &--crop {\n box-shadow: 0 0 0 100vmax\n color-mix(in srgb, var(--color-black) 55%, transparent);\n }\n\n &--cover {\n border-style: dashed;\n background-color: color-mix(\n in srgb,\n var(--color-black) 30%,\n transparent\n );\n }\n }\n\n // The text being typed, where it will be drawn: from the point\n // clicked to the right edge of the image\n &__text {\n position: absolute;\n right: 0;\n display: block;\n margin: 0;\n padding: 0;\n overflow: hidden;\n border: 0;\n outline: 1px dashed\n color-mix(in srgb, var(--color-white) 80%, transparent);\n background: transparent;\n font-weight: 700;\n white-space: pre-wrap;\n overflow-wrap: anywhere;\n resize: none;\n caret-color: currentcolor;\n user-select: text;\n text-shadow:\n 0 0 1px var(--pk-image-editor-text-edge),\n 0 0 2px var(--pk-image-editor-text-edge),\n 0 0 3px var(--pk-image-editor-text-edge);\n }\n }\n\n // A finger gets 40px (DESIGN.md): a segment is drawn 32px tall, and its\n // area grows around it, as `u-touch-target` does\n @media (pointer: coarse) {\n .pk-image-editor .vv-radio--segment::after {\n content: '';\n position: absolute;\n top: 50%;\n left: 50%;\n width: max(100%, var(--spacing-40));\n height: max(100%, var(--spacing-40));\n transform: translate(-50%, -50%);\n }\n }\n\n // Same as `u-visually-hidden`, which a class cannot get from the width\n // of a container. Under 50rem the named tools pushed undo and redo to a\n // row of their own (760px, 2026-11-04)\n @container pk-image-editor (max-width: 50rem) {\n .pk-image-editor__tool-label {\n position: absolute;\n width: 1px;\n height: 1px;\n overflow: hidden;\n clip-path: inset(50%);\n white-space: nowrap;\n }\n }\n</style>\n","<script setup lang=\"ts\">\n import {\n computed,\n nextTick,\n onBeforeUnmount,\n onMounted,\n ref,\n shallowRef,\n useTemplateRef,\n } from 'vue'\n import { useI18n } from 'vue-i18n'\n import { useMediaQuery } from '@vueuse/core'\n import PkImageEditor from './PkImageEditor.vue'\n import {\n editedFileName,\n exportCanvas,\n loadEditableImage,\n releaseCanvas,\n sizeAfterEdits,\n type EditorTool,\n type ImageEdit,\n type LoadedImage,\n } from './imageEdits'\n\n /**\n * The image editor in a dialog (spec `chat-image-editor.md`): opens on an\n * image file and gives back the edited copy, same name and type. Open\n * while it is mounted: `close` asks the host to drop it. It keeps open on\n * a click outside and on `Esc`, which would throw the edits away.\n */\n const props = defineProps<{\n /**\n * The image: a file of the composer, or one still on its way, such\n * as an image of the conversation the lightbox reads.\n */\n file: File | Promise<File>\n /** The tool picked on opening, crop if none. */\n initialTool?: EditorTool\n }>()\n\n const emit = defineEmits<{\n close: []\n /** The edited copy of `file`. */\n apply: [file: File]\n }>()\n\n const { t: $t } = useI18n({ useScope: 'global' })\n\n // The phone breakpoint of the design system: the dialog covers the screen\n const fullscreen = useMediaQuery('(max-width: 575.98px)')\n\n // A canvas is no state to make deeply reactive\n const source = shallowRef<File>()\n const loaded = shallowRef<LoadedImage>()\n const failed = ref<'load' | 'draw' | 'export'>()\n const edits = ref<ImageEdit[]>([])\n const exporting = ref(false)\n const editorEl =\n useTemplateRef<InstanceType<typeof PkImageEditor>>('editorEl')\n let unmounted = false\n\n onMounted(async () => {\n try {\n const file = await props.file\n const image = await loadEditableImage(file)\n // Closed while the image arrived: nothing will release it later\n if (unmounted) {\n releaseCanvas(image.image)\n return\n }\n source.value = file\n loaded.value = image\n } catch {\n failed.value = 'load'\n }\n })\n\n onBeforeUnmount(() => {\n unmounted = true\n if (loaded.value) {\n releaseCanvas(loaded.value.image)\n }\n })\n\n const size = computed(\n () => loaded.value && sizeAfterEdits(loaded.value.image, edits.value),\n )\n const scaledDown = computed(\n () =>\n !!loaded.value &&\n loaded.value.image.width < loaded.value.original.width,\n )\n const changed = computed(\n () => edits.value.length > 0 || !!editorEl.value?.pending,\n )\n\n async function useImage() {\n const editor = editorEl.value\n const file = source.value\n if (!editor || !file) {\n return\n }\n // A crop drawn and not applied, a text being typed: what the user\n // sees goes too\n editor.applyPending()\n // A crop of the whole image changes nothing\n if (!edits.value.length) {\n emit('close')\n return\n }\n failed.value = undefined\n exporting.value = true\n try {\n // The editor draws the crop above in its next update; the file is\n // written from that canvas, not from a second one of full size\n await nextTick()\n const canvas = editor.shownCanvas()\n if (failed.value === 'draw') {\n return\n }\n if (!canvas) {\n throw new Error('No image drawn')\n }\n const blob = await exportCanvas(canvas, file.type)\n emit(\n 'apply',\n new File(\n [blob],\n editedFileName(file.name, file.type, blob.type),\n { type: blob.type, lastModified: Date.now() },\n ),\n )\n } catch {\n failed.value = 'export'\n } finally {\n exporting.value = false\n }\n }\n\n const failedLabel = computed(() => {\n switch (failed.value) {\n case 'export':\n return $t('imageEditor.exportFailed')\n case 'draw':\n return $t('imageEditor.drawFailed')\n default:\n return $t('imageEditor.loadFailed')\n }\n })\n</script>\n\n<template>\n <VvDialog\n :model-value=\"true\"\n :title=\"$t('imageEditor.title')\"\n :modifiers=\"fullscreen ? 'fullscreen' : undefined\"\n keep-open\n class=\"pk-image-editor-dialog\"\n @update:model-value=\"!$event && emit('close')\">\n <VvAlert\n v-if=\"failed\"\n modifiers=\"danger\"\n class=\"m-md\"\n :content=\"failedLabel\" />\n <PkImageEditor\n v-if=\"loaded && failed !== 'draw'\"\n ref=\"editorEl\"\n v-model:edits=\"edits\"\n :image=\"loaded.image\"\n :initial-tool=\"initialTool\"\n @error=\"failed = 'draw'\" />\n <div\n v-else-if=\"!failed\"\n class=\"pk-image-editor-dialog__loading\"\n role=\"status\"\n :aria-label=\"$t('imageEditor.loading')\">\n <VvIcon name=\"loading-loop\" />\n </div>\n <p v-if=\"size\" class=\"pk-image-editor-dialog__size\">\n {{\n scaledDown\n ? $t('imageEditor.sizeScaledDown', {\n width: size.width,\n height: size.height,\n })\n : $t('imageEditor.size', {\n width: size.width,\n height: size.height,\n })\n }}\n </p>\n <template #footer>\n <VvButtonGroup>\n <VvButton\n modifiers=\"secondary\"\n :label=\"$t('action.cancel')\"\n @click=\"emit('close')\" />\n <VvButton\n :label=\"$t('imageEditor.use')\"\n :loading=\"exporting\"\n :disabled=\"exporting || !changed || failed === 'draw'\"\n @click=\"useImage\" />\n </VvButtonGroup>\n </template>\n </VvDialog>\n</template>\n\n<style lang=\"scss\">\n // Most of the window, as the file viewer: room for a photo and the tools\n .pk-image-editor-dialog {\n &:not(.vv-dialog--fullscreen) > article {\n width: min(64rem, 94dvw);\n height: min(48rem, 92dvh);\n }\n\n // The library writes `max-height: auto`, which is no value (see\n // PkFilePreviewDialog)\n &.vv-dialog--fullscreen > article {\n max-height: none;\n }\n\n .vv-dialog__content {\n display: flex;\n flex: 1;\n flex-direction: column;\n min-height: 0;\n padding: 0;\n }\n\n &__loading {\n display: flex;\n flex: 1;\n align-items: center;\n justify-content: center;\n color: var(--color-word-3);\n font-size: var(--text-24);\n }\n\n &__size {\n margin: 0;\n padding: var(--spacing-8) var(--spacing-md);\n border-top: 1px solid var(--color-surface-3);\n color: var(--color-word-3);\n font-size: var(--text-12);\n font-variant-numeric: tabular-nums;\n }\n }\n</style>\n","<script setup lang=\"ts\">\n import {\n computed,\n nextTick,\n onBeforeUnmount,\n onMounted,\n ref,\n shallowRef,\n useTemplateRef,\n } from 'vue'\n import { useI18n } from 'vue-i18n'\n import { useMediaQuery } from '@vueuse/core'\n import PkImageEditor from './PkImageEditor.vue'\n import {\n editedFileName,\n exportCanvas,\n loadEditableImage,\n releaseCanvas,\n sizeAfterEdits,\n type EditorTool,\n type ImageEdit,\n type LoadedImage,\n } from './imageEdits'\n\n /**\n * The image editor in a dialog (spec `chat-image-editor.md`): opens on an\n * image file and gives back the edited copy, same name and type. Open\n * while it is mounted: `close` asks the host to drop it. It keeps open on\n * a click outside and on `Esc`, which would throw the edits away.\n */\n const props = defineProps<{\n /**\n * The image: a file of the composer, or one still on its way, such\n * as an image of the conversation the lightbox reads.\n */\n file: File | Promise<File>\n /** The tool picked on opening, crop if none. */\n initialTool?: EditorTool\n }>()\n\n const emit = defineEmits<{\n close: []\n /** The edited copy of `file`. */\n apply: [file: File]\n }>()\n\n const { t: $t } = useI18n({ useScope: 'global' })\n\n // The phone breakpoint of the design system: the dialog covers the screen\n const fullscreen = useMediaQuery('(max-width: 575.98px)')\n\n // A canvas is no state to make deeply reactive\n const source = shallowRef<File>()\n const loaded = shallowRef<LoadedImage>()\n const failed = ref<'load' | 'draw' | 'export'>()\n const edits = ref<ImageEdit[]>([])\n const exporting = ref(false)\n const editorEl =\n useTemplateRef<InstanceType<typeof PkImageEditor>>('editorEl')\n let unmounted = false\n\n onMounted(async () => {\n try {\n const file = await props.file\n const image = await loadEditableImage(file)\n // Closed while the image arrived: nothing will release it later\n if (unmounted) {\n releaseCanvas(image.image)\n return\n }\n source.value = file\n loaded.value = image\n } catch {\n failed.value = 'load'\n }\n })\n\n onBeforeUnmount(() => {\n unmounted = true\n if (loaded.value) {\n releaseCanvas(loaded.value.image)\n }\n })\n\n const size = computed(\n () => loaded.value && sizeAfterEdits(loaded.value.image, edits.value),\n )\n const scaledDown = computed(\n () =>\n !!loaded.value &&\n loaded.value.image.width < loaded.value.original.width,\n )\n const changed = computed(\n () => edits.value.length > 0 || !!editorEl.value?.pending,\n )\n\n async function useImage() {\n const editor = editorEl.value\n const file = source.value\n if (!editor || !file) {\n return\n }\n // A crop drawn and not applied, a text being typed: what the user\n // sees goes too\n editor.applyPending()\n // A crop of the whole image changes nothing\n if (!edits.value.length) {\n emit('close')\n return\n }\n failed.value = undefined\n exporting.value = true\n try {\n // The editor draws the crop above in its next update; the file is\n // written from that canvas, not from a second one of full size\n await nextTick()\n const canvas = editor.shownCanvas()\n if (failed.value === 'draw') {\n return\n }\n if (!canvas) {\n throw new Error('No image drawn')\n }\n const blob = await exportCanvas(canvas, file.type)\n emit(\n 'apply',\n new File(\n [blob],\n editedFileName(file.name, file.type, blob.type),\n { type: blob.type, lastModified: Date.now() },\n ),\n )\n } catch {\n failed.value = 'export'\n } finally {\n exporting.value = false\n }\n }\n\n const failedLabel = computed(() => {\n switch (failed.value) {\n case 'export':\n return $t('imageEditor.exportFailed')\n case 'draw':\n return $t('imageEditor.drawFailed')\n default:\n return $t('imageEditor.loadFailed')\n }\n })\n</script>\n\n<template>\n <VvDialog\n :model-value=\"true\"\n :title=\"$t('imageEditor.title')\"\n :modifiers=\"fullscreen ? 'fullscreen' : undefined\"\n keep-open\n class=\"pk-image-editor-dialog\"\n @update:model-value=\"!$event && emit('close')\">\n <VvAlert\n v-if=\"failed\"\n modifiers=\"danger\"\n class=\"m-md\"\n :content=\"failedLabel\" />\n <PkImageEditor\n v-if=\"loaded && failed !== 'draw'\"\n ref=\"editorEl\"\n v-model:edits=\"edits\"\n :image=\"loaded.image\"\n :initial-tool=\"initialTool\"\n @error=\"failed = 'draw'\" />\n <div\n v-else-if=\"!failed\"\n class=\"pk-image-editor-dialog__loading\"\n role=\"status\"\n :aria-label=\"$t('imageEditor.loading')\">\n <VvIcon name=\"loading-loop\" />\n </div>\n <p v-if=\"size\" class=\"pk-image-editor-dialog__size\">\n {{\n scaledDown\n ? $t('imageEditor.sizeScaledDown', {\n width: size.width,\n height: size.height,\n })\n : $t('imageEditor.size', {\n width: size.width,\n height: size.height,\n })\n }}\n </p>\n <template #footer>\n <VvButtonGroup>\n <VvButton\n modifiers=\"secondary\"\n :label=\"$t('action.cancel')\"\n @click=\"emit('close')\" />\n <VvButton\n :label=\"$t('imageEditor.use')\"\n :loading=\"exporting\"\n :disabled=\"exporting || !changed || failed === 'draw'\"\n @click=\"useImage\" />\n </VvButtonGroup>\n </template>\n </VvDialog>\n</template>\n\n<style lang=\"scss\">\n // Most of the window, as the file viewer: room for a photo and the tools\n .pk-image-editor-dialog {\n &:not(.vv-dialog--fullscreen) > article {\n width: min(64rem, 94dvw);\n height: min(48rem, 92dvh);\n }\n\n // The library writes `max-height: auto`, which is no value (see\n // PkFilePreviewDialog)\n &.vv-dialog--fullscreen > article {\n max-height: none;\n }\n\n .vv-dialog__content {\n display: flex;\n flex: 1;\n flex-direction: column;\n min-height: 0;\n padding: 0;\n }\n\n &__loading {\n display: flex;\n flex: 1;\n align-items: center;\n justify-content: center;\n color: var(--color-word-3);\n font-size: var(--text-24);\n }\n\n &__size {\n margin: 0;\n padding: var(--spacing-8) var(--spacing-md);\n border-top: 1px solid var(--color-surface-3);\n color: var(--color-word-3);\n font-size: var(--text-12);\n font-variant-numeric: tabular-nums;\n }\n }\n</style>\n"],"mappings":";;;;;;;AAoCA,IAAayC,IAGT;CACA,KAAK;EAAE,MAAM;EAAW,MAAM;CAAU;CACxC,QAAQ;EAAE,MAAM;EAAW,MAAM;CAAU;CAC3C,OAAO;EAAE,MAAM;EAAW,MAAM;CAAU;CAC1C,MAAM;EAAE,MAAM;EAAW,MAAM;CAAU;CACzC,OAAO;EAAE,MAAM;EAAW,MAAM;CAAU;CAC1C,OAAO;EAAE,MAAM;EAAW,MAAM;CAAU;AAC9C,GAGaC,KACT,gFAkDSC,IAAkB,UAGlBC,KAAiB,KAOxBC,IAAkB,IAClBC,IAAsB,IAOtBC,IAAa,GACbC,IAAiB,KACjBC,KAAgB,IAChBC,KAAoB,IAEpBC,KAAoB,IAGbC,KAAmB,KAG1BC,KAAoB,GACpBC,KAAwB,KAExBC,KAAmB,IAEnBC,KAAqC;CACvC,cAAc;CACd,aAAa;CACb,cAAc;AAClB;AAMA,SAAgBC,EACZ,GACA,IAAUd,GACD;CACT,IAAMe,IAAOC,EAAK,QAAQA,EAAK;CAC/B,IAAID,KAAQE,GACR,OAAO;EAAE,OAAOD,EAAK;EAAO,QAAQA,EAAK;CAAO;CAEpD,IAAME,IAAQ,KAAK,KAAKD,IAAUF,CAAI;CACtC,OAAO;EACH,OAAO,KAAK,IAAI,GAAG,KAAK,MAAMC,EAAK,QAAQE,CAAK,CAAC;EACjD,QAAQ,KAAK,IAAI,GAAG,KAAK,MAAMF,EAAK,SAASE,CAAK,CAAC;CACvD;AACJ;AAGA,SAAgBC,GACZ,GACA,GACS;CACT,IAAI,EAAE,UAAO,cAAWH;CACxB,KAAK,IAAMI,KAAQC,GACf,AAAID,EAAK,SAAS,WACb,CAACE,GAAOC,KAAU,CAACA,GAAQD,CAAK,IAC1BF,EAAK,SAAS,WACnB,wBAA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,UAAM;MACzC,SAAOM;;;;;;;;IA1CpB,SAAA9Q,QAI6B;KAHnBwQ,EAAA,SADV3Q,EAAA,GAAAC,EAI6BmR,GAAA;;MAFzB,WAAU;MACV,OAAM;MACL,SAASD,EAAA;;KAEJT,EAAA,SAAUC,EAAA,UAAM,UAD1B3Q,EAAA,GAAAC,EAM+BK,GAAA;;MAJvB,SAAA;MAAJ,KAAIuQ;MACI,OAAO3O,EAAA;MAAA,kBAAA,AAAAmN,EAAA,QAAA,MAAA,EAAK,QAAAC;MACnB,OAAOoB,EAAA,MAAO;MACd,gBAAcvG,EAAA;MACd,SAAK,AAAAkF,EAAA,QAAA,MAAE,EAAA,QAAM;;;;;K,MAEFsB,EAAA,Q,aADhB3Q,EAAA,GAAAO,EAMM,OAAA;;MAJF,OAAM;MACN,MAAK;MACJ,cAAYL,EAAAoK,CAAA,CAAE,CAAA,qBAAA;K,IACflK,EAA8BuO,IAAA,EAAtB,MAAK,eAAc,CAAA,C;KAEtB9M,EAAA,SAAT7B,EAAA,GAAAO,EAYI,KAZJ+P,IAYI9P,EAVIuQ,GAAA,QAAiC7Q,EAAAoK,CAAA,CAAE,CAAA,8BAAA;MAAkE,OAAAzI,EAAA,MAAK;MAAyC,QAAAA,EAAA,MAAK;K,KAAuD3B,EAAAoK,CAAA,CAAE,CAAA,oBAAA;MAAwD,OAAAzI,EAAA,MAAK;MAAyC,QAAAA,EAAA,MAAK"}
@@ -1,5 +1,5 @@
1
1
  import { W as e } from "./Agent-B55xNURC.js";
2
- import { a as t, i as n, o as r } from "./useLightbox-FDyQrPWe.js";
2
+ import { a as t, o as n, s as r } from "./useLightbox-Ctl9zaMX.js";
3
3
  import "./src-nb8MU1sh.js";
4
4
  import { _ as i } from "./utils-DXOZ6zjh.js";
5
5
  import { i as a, n as o } from "./url-validation-CJV9pp4v.js";
@@ -3284,7 +3284,7 @@ var ja = ["data-pswp-images", "innerHTML"], Ma = 100, Na = /* @__PURE__ */ u({
3284
3284
  pe,
3285
3285
  ln,
3286
3286
  fe
3287
- ], S = re("container"), he = v(te(a, "markdown"), Ma), ge = c(() => a.tailLength ? i(he.value, a.tailLength) : he.value), { resolveText: _e } = s(), ve = _e(ge), C = Aa(), ye = n(), be = c(() => {
3287
+ ], S = re("container"), he = v(te(a, "markdown"), Ma), ge = c(() => a.tailLength ? i(he.value, a.tailLength) : he.value), { resolveText: _e } = s(), ve = _e(ge), C = Aa(), ye = t(), be = c(() => {
3288
3288
  let e = me.reduce((e, t) => t(e), ve.value);
3289
3289
  return wa(W.parse(e, {
3290
3290
  async: !1,
@@ -3326,18 +3326,18 @@ var ja = ["data-pswp-images", "innerHTML"], Ma = 100, Na = /* @__PURE__ */ u({
3326
3326
  await f();
3327
3327
  let e = S.value?.querySelectorAll(".katex-pending");
3328
3328
  if (!e?.length) return;
3329
- let n = await ae();
3330
- r("https://cdn.jsdelivr.net/npm/katex@0.18/dist/katex.min.css", t(S.value));
3331
- for (let t of Array.from(e)) {
3332
- if (!t.isConnected) continue;
3333
- let e = t.dataset.katex;
3329
+ let t = await ae();
3330
+ r("https://cdn.jsdelivr.net/npm/katex@0.18/dist/katex.min.css", n(S.value));
3331
+ for (let n of Array.from(e)) {
3332
+ if (!n.isConnected) continue;
3333
+ let e = n.dataset.katex;
3334
3334
  if (!e) continue;
3335
- let r = t.dataset.display === "true";
3335
+ let r = n.dataset.display === "true";
3336
3336
  try {
3337
- t.innerHTML = n.renderToString(decodeURIComponent(e), {
3337
+ n.innerHTML = t.renderToString(decodeURIComponent(e), {
3338
3338
  throwOnError: !1,
3339
3339
  displayMode: r
3340
- }), t.classList.remove("katex-pending");
3340
+ }), n.classList.remove("katex-pending");
3341
3341
  } catch {}
3342
3342
  }
3343
3343
  }
@@ -3426,4 +3426,4 @@ var ja = ["data-pswp-images", "innerHTML"], Ma = 100, Na = /* @__PURE__ */ u({
3426
3426
  });
3427
3427
  export { Ea as n, Aa as r, Na as t };
3428
3428
 
3429
- //# sourceMappingURL=PkStreamingMarkdown-B3VWOaWb.js.map
3429
+ //# sourceMappingURL=PkStreamingMarkdown-DdOv86Eb.js.map