mirror of
https://github.com/penpot/penpot.git
synced 2026-11-26 04:40:33 +00:00
* 🐛 Create Plugin API shapes at the page root Create component instances and SVG shapes at the page root so a board at the page origin cannot capture them. Continue using the existing forced frame option for component instances and an explicit parent frame for SVG imports. Interactive placement retains its current behavior. Cover component instances, SVG imports and SVG imports with images on ordinary, flex and grid origin boards, including undo, redo, position changes and explicit reparenting. Closes #12086 AI-assisted-by: deepseek-v4.1-flash Signed-off-by: choi junsoo <junsoo1172@gmail.com> * 🐛 Preserve interactive creation placement Cover component and SVG placement inside and outside an origin board. Exercise the sidebar move lifecycle and verify parent preservation. Correct the Plugin API changelog category. AI-assisted-by: gpt-6 Signed-off-by: choi junsoo <junsoo1172@gmail.com> * 🔧 Cover page-root creation in the Plugin API test suite Check that LibraryComponent.instance(), createShapeFromSvg() and createShapeFromSvgWithImages() create shapes at the page root when a board covers the page origin. Each test removes the shape it creates, since it lives outside the scratch board. AI-assisted-by: claude-opus-5-5 --------- Signed-off-by: choi junsoo <junsoo1172@gmail.com> Co-authored-by: alonso.torres <alonso.torres@kaleidos.net>
133 lines
5.9 KiB
Clojure
133 lines
5.9 KiB
Clojure
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
;;
|
|
;; Copyright (c) KALEIDOS SUBSIDIARY SL
|
|
|
|
(ns app.main.data.workspace.svg-upload
|
|
(:require
|
|
[app.common.data :as d]
|
|
[app.common.data.macros :as dm]
|
|
[app.common.exceptions :as ex]
|
|
[app.common.files.changes-builder :as pcb]
|
|
[app.common.files.helpers :as cfh]
|
|
[app.common.files.shapes-builder :as sb]
|
|
[app.common.svg :as csvg]
|
|
[app.common.types.shape-tree :as ctst]
|
|
[app.common.uuid :as uuid]
|
|
[app.main.data.changes :as dch]
|
|
[app.main.data.helpers :as dsh]
|
|
[app.main.data.workspace.selection :as dws]
|
|
[app.main.data.workspace.undo :as dwu]
|
|
[app.main.repo :as rp]
|
|
[app.util.webapi :as wapi]
|
|
[beicon.v2.core :as rx]
|
|
[cuerdas.core :as str]
|
|
[potok.v2.core :as ptk]))
|
|
|
|
(defn extract-name [href]
|
|
(let [query-idx (str/last-index-of href "?")
|
|
href (if (> query-idx 0) (subs href 0 query-idx) href)
|
|
filename (->> (str/split href "/") (last))
|
|
ext-idx (str/last-index-of filename ".")]
|
|
(if (> ext-idx 0) (subs filename 0 ext-idx) filename)))
|
|
|
|
(defn upload-images
|
|
"Extract all bitmap images inside the svg data, and upload them, associated to the file.
|
|
Return a map {<href> <image-data>}."
|
|
[svg-data file-id]
|
|
(->> (rx/from (csvg/collect-images svg-data))
|
|
(rx/map (fn [{:keys [href] :as item}]
|
|
(let [item (-> item
|
|
(assoc :file-id file-id)
|
|
(assoc :is-local true)
|
|
(assoc :name "image"))]
|
|
(if (str/starts-with? href "data:")
|
|
(assoc item :content (wapi/data-uri->blob href))
|
|
(-> item
|
|
(assoc :name (extract-name href))
|
|
(assoc :url href))))))
|
|
(rx/filter (fn [item]
|
|
(or (contains? item :content)
|
|
(let [url (:url item)]
|
|
(or (str/starts-with? url "http://")
|
|
(str/starts-with? url "https://"))))))
|
|
(rx/mapcat (fn [item]
|
|
;; The upload commands use closed params schemas, so
|
|
;; drop the svg-only attrs (:href :width :height) that
|
|
;; `svg/collect-images` attaches.
|
|
(->> (rp/cmd! (if (contains? item :content)
|
|
:upload-file-media-object
|
|
:create-file-media-object-from-url)
|
|
(dissoc item :href :width :height))
|
|
;; When the image uploaded fail we skip the shape
|
|
;; returning `nil` will afterward not create the shape.
|
|
(rx/catch #(rx/of nil))
|
|
(rx/map #(vector (:href item) %)))))
|
|
(rx/reduce conj {})))
|
|
|
|
(defn add-svg-shapes
|
|
([svg-data position]
|
|
(add-svg-shapes nil svg-data position nil))
|
|
|
|
([id svg-data position {:keys [change-selection? ignore-selection? frame-id]
|
|
:or {ignore-selection? false change-selection? true}}]
|
|
(ptk/reify ::add-svg-shapes
|
|
ptk/WatchEvent
|
|
(watch [it state _]
|
|
(try
|
|
(let [id (d/nilv id (uuid/next))
|
|
page-id (:current-page-id state)
|
|
objects (dsh/lookup-page-objects state page-id)
|
|
selected (if ignore-selection? #{} (dsh/lookup-selected state))
|
|
base (cfh/get-base-shape objects selected)
|
|
|
|
selected-id (first selected)
|
|
selected-frame? (and (= 1 (count selected))
|
|
(= :frame (dm/get-in objects [selected-id :type])))
|
|
|
|
base-id (:parent-id base)
|
|
|
|
frame-id (or frame-id
|
|
(if (or selected-frame? (empty? selected)
|
|
(not= :frame (dm/get-in objects [base-id :type])))
|
|
(ctst/top-nested-frame objects position)
|
|
base-id))
|
|
|
|
parent-id (if (or selected-frame? (empty? selected) (nil? base))
|
|
frame-id
|
|
base-id)
|
|
|
|
[new-shape new-children]
|
|
(sb/create-svg-shapes id svg-data position objects frame-id parent-id selected true)
|
|
|
|
changes (-> (pcb/empty-changes it page-id)
|
|
(pcb/with-objects objects)
|
|
(pcb/add-object new-shape))
|
|
|
|
changes (reduce (fn [changes new-child]
|
|
(pcb/add-object changes new-child))
|
|
changes
|
|
new-children)
|
|
|
|
changes (pcb/resize-parents changes
|
|
(->> (:redo-changes changes)
|
|
(filter #(= :add-obj (:type %)))
|
|
(map :id)
|
|
(reverse)
|
|
(vec)))
|
|
undo-id (js/Symbol)]
|
|
|
|
(rx/of (dwu/start-undo-transaction undo-id)
|
|
(dch/commit-changes changes)
|
|
(when change-selection?
|
|
(dws/select-shapes (d/ordered-set (:id new-shape))))
|
|
(ptk/data-event :layout/update {:ids [(:id new-shape)]})
|
|
(dwu/commit-undo-transaction undo-id)))
|
|
|
|
(catch :default cause
|
|
(js/console.error cause)
|
|
(rx/throw (ex/error :type :svg-parser
|
|
:hint (ex-message cause)))))))))
|
|
|