Skip to content

Commit

Permalink
fix: internal blocks paste
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin authored and andelf committed Mar 29, 2023
1 parent 07a3adc commit bc635ea
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 100 deletions.
3 changes: 2 additions & 1 deletion src/main/frontend/components/export.cljs
Expand Up @@ -207,5 +207,6 @@
[:div.mt-4
(ui/button (if @*copied? "Copied to clipboard!" "Copy to clipboard")
:on-click (fn []
(util/copy-to-clipboard! @*content (when (= tp :html) @*content))
(util/copy-to-clipboard! @*content
:html (when (= tp :html) @*content))
(reset! *copied? true)))]]))
4 changes: 2 additions & 2 deletions src/main/frontend/extensions/pdf/assets.cljs
Expand Up @@ -213,8 +213,8 @@
[highlight ^js viewer]
(when-let [ref-block (ensure-ref-block! (state/get-current-pdf) highlight)]
(util/copy-to-clipboard!
(block-ref/->block-ref (:block/uuid ref-block)) nil
(pdf-windows/resolve-own-window viewer))))
(block-ref/->block-ref (:block/uuid ref-block))
:owner-window (pdf-windows/resolve-own-window viewer))))

(defn open-block-ref!
[block]
Expand Down
4 changes: 2 additions & 2 deletions src/main/frontend/extensions/pdf/core.cljs
Expand Up @@ -141,8 +141,8 @@
"copy"
(do
(util/copy-to-clipboard!
(or (:text content) (pdf-utils/fix-selection-text-breakline (.toString selection))) nil
(pdf-windows/resolve-own-window viewer))
(or (:text content) (pdf-utils/fix-selection-text-breakline (.toString selection)))
:owner-window (pdf-windows/resolve-own-window viewer))
(pdf-utils/clear-all-selection))

"link"
Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/extensions/tldraw.cljs
Expand Up @@ -94,7 +94,7 @@
:isMobile util/mobile?
:saveAsset save-asset-handler
:makeAssetUrl editor-handler/make-asset-url
:copyToClipboard (fn [text, html] (util/copy-to-clipboard! text html))
:copyToClipboard (fn [text, html] (util/copy-to-clipboard! text :html html))
:getRedirectPageName (fn [page-name-or-uuid] (model/get-redirect-page-name page-name-or-uuid))
:insertFirstPageBlock (fn [page-name] (editor-handler/insert-first-page-block-if-not-exists! page-name {:redirect? false}))
:addNewWhiteboard (fn [page-name]
Expand Down
6 changes: 4 additions & 2 deletions src/main/frontend/handler/common.cljs
Expand Up @@ -11,8 +11,10 @@
["ignore" :as Ignore]))

(defn copy-to-clipboard-without-id-property!
[format raw-text html]
(util/copy-to-clipboard! (property/remove-id-property format raw-text) html))
[format raw-text html blocks]
(util/copy-to-clipboard! (property/remove-id-property format raw-text)
:html html
:blocks blocks))

(defn config-with-document-mode
[config]
Expand Down
15 changes: 8 additions & 7 deletions src/main/frontend/handler/editor.cljs
Expand Up @@ -1010,9 +1010,10 @@
[top-level-block-uuids content] (compose-copied-blocks-contents repo ids)
block (db/entity [:block/uuid (first ids)])]
(when block
(let [html (export-html/export-blocks-as-html repo top-level-block-uuids nil)]
(common-handler/copy-to-clipboard-without-id-property! (:block/format block) content (when html? html)))
(state/set-copied-blocks! content (get-all-blocks-by-ids repo top-level-block-uuids))
(let [html (export-html/export-blocks-as-html repo top-level-block-uuids nil)
copied-blocks (get-all-blocks-by-ids repo top-level-block-uuids)]
(common-handler/copy-to-clipboard-without-id-property! (:block/format block) content (when html? html) copied-blocks))
;; (state/set-copied-blocks! content (get-all-blocks-by-ids repo top-level-block-uuids))
(notification/show! "Copied!" :success)))))

(defn copy-block-refs
Expand Down Expand Up @@ -1228,8 +1229,8 @@
[_top-level-block-uuids md-content] (compose-copied-blocks-contents repo [block-id])
html (export-html/export-blocks-as-html repo [block-id] nil)
sorted-blocks (tree/get-sorted-block-and-children repo (:db/id block))]
(state/set-copied-blocks! md-content sorted-blocks)
(common-handler/copy-to-clipboard-without-id-property! (:block/format block) md-content html)
;; (state/set-copied-blocks! md-content sorted-blocks)
(common-handler/copy-to-clipboard-without-id-property! (:block/format block) md-content html sorted-blocks)
(delete-block-aux! block true))))

(defn clear-last-selected-block!
Expand Down Expand Up @@ -1510,7 +1511,7 @@
;; assets/journals_2021_02_03_1612350230540_0.png
(defn resolve-relative-path
"Relative path to current file path.
Requires editing state"
[file-path]
(if-let [current-file-rpath (or (db-model/get-block-file-path (state/get-edit-block))
Expand Down Expand Up @@ -2052,7 +2053,7 @@
(let [format (or (:block/format target-block') (state/get-preferred-format))
blocks' (map (fn [block]
(paste-block-cleanup block page exclude-properties format content-update-fn keep-uuid?))
blocks)
blocks)
result (outliner-core/insert-blocks! blocks' target-block' {:sibling? sibling?
:outliner-op :paste
:replace-empty-target? replace-empty-target?
Expand Down
148 changes: 74 additions & 74 deletions src/main/frontend/handler/paste.cljs
Expand Up @@ -18,7 +18,8 @@
[cljs.core.match :refer [match]]
[frontend.util.text :as text-util]
[frontend.format.mldoc :as mldoc]
[lambdaisland.glogi :as log]))
[lambdaisland.glogi :as log]
[promesa.core :as p]))

(defn- paste-text-parseable
[format text]
Expand Down Expand Up @@ -85,79 +86,78 @@
;; todo: logseq/whiteboard-shapes is now text/html
[text e html]
(util/stop e)
(let [copied-blocks (state/get-copied-blocks)
input (state/get-input)
input-id (state/get-edit-input-id)
text (string/replace text "\r\n" "\n") ;; Fix for Windows platform
replace-text-f (fn [text]
(commands/delete-selection! input-id)
(commands/simple-insert! input-id text nil))
internal-paste? (and
(seq (:copy/blocks copied-blocks))
;; not copied from the external clipboard
(= (string/trimr text)
(string/trimr (:copy/content copied-blocks))))]
(if internal-paste?
(let [blocks (:copy/blocks copied-blocks)]
(when (seq blocks)
(editor-handler/paste-blocks blocks {})))
(let [shape-refs-text (when (and (not (string/blank? html))
(get-whiteboard-tldr-from-text html))
;; text should always be prepared block-ref generated in tldr
text)
{:keys [value selection] :as selection-and-format} (editor-handler/get-selection-and-format)
text-url? (gp-util/url? text)
selection-url? (gp-util/url? selection)]
(cond
(not (string/blank? shape-refs-text))
(commands/simple-insert! input-id shape-refs-text nil)

(or (and (or text-url? selection-url?)
(selection-within-link? selection-and-format))
(and text-url? selection-url?))
(replace-text-f text)

(and (or text-url?
(and value (gp-util/url? (string/trim value))))
(not (string/blank? (util/get-selected-text))))
(editor-handler/html-link-format! text)

(and (block-ref/block-ref? text)
(editor-handler/wrapped-by? input block-ref/left-parens block-ref/right-parens))
(commands/simple-insert! input-id (block-ref/get-block-ref-id text) nil)

:else
;; from external
(let [format (or (db/get-page-format (state/get-current-page)) :markdown)
html-text (let [result (when-not (string/blank? html)
(try
(html-parser/convert format html)
(catch :default e
(log/error :exception e)
nil)))]
(if (string/blank? result) nil result))
text (or html-text text)]
(match [format
(nil? (util/safe-re-find #"(?m)^\s*(?:[-+*]|#+)\s+" text))
(nil? (util/safe-re-find #"(?m)^\s*\*+\s+" text))
(nil? (util/safe-re-find #"(?:\r?\n){2,}" text))]
[:markdown false _ _]
(paste-text-parseable format text)

[:org _ false _]
(paste-text-parseable format text)

[:markdown true _ false]
(paste-segmented-text format text)

[:markdown true _ true]
(replace-text-f text)

[:org _ true false]
(paste-segmented-text format text)

[:org _ true true]
(replace-text-f text))))))))
(p/let [clipboard-items (js/navigator.clipboard.read)
blocks-blob ^js (.getType ^js (first clipboard-items)
"web application/logseq")
blocks-str (when blocks-blob (.text blocks-blob))
copied-blocks (when blocks-str
(gp-util/safe-read-string blocks-str))]
(let [input (state/get-input)
input-id (state/get-edit-input-id)
text (string/replace text "\r\n" "\n") ;; Fix for Windows platform
replace-text-f (fn [text]
(commands/delete-selection! input-id)
(commands/simple-insert! input-id text nil))
internal-paste? (seq copied-blocks)]
(if internal-paste?
(editor-handler/paste-blocks copied-blocks {})
(let [shape-refs-text (when (and (not (string/blank? html))
(get-whiteboard-tldr-from-text html))
;; text should always be prepared block-ref generated in tldr
text)
{:keys [value selection] :as selection-and-format} (editor-handler/get-selection-and-format)
text-url? (gp-util/url? text)
selection-url? (gp-util/url? selection)]
(cond
(not (string/blank? shape-refs-text))
(commands/simple-insert! input-id shape-refs-text nil)

(or (and (or text-url? selection-url?)
(selection-within-link? selection-and-format))
(and text-url? selection-url?))
(replace-text-f text)

(and (or text-url?
(and value (gp-util/url? (string/trim value))))
(not (string/blank? (util/get-selected-text))))
(editor-handler/html-link-format! text)

(and (block-ref/block-ref? text)
(editor-handler/wrapped-by? input block-ref/left-parens block-ref/right-parens))
(commands/simple-insert! input-id (block-ref/get-block-ref-id text) nil)

:else
;; from external
(let [format (or (db/get-page-format (state/get-current-page)) :markdown)
html-text (let [result (when-not (string/blank? html)
(try
(html-parser/convert format html)
(catch :default e
(log/error :exception e)
nil)))]
(if (string/blank? result) nil result))
text (or html-text text)]
(match [format
(nil? (util/safe-re-find #"(?m)^\s*(?:[-+*]|#+)\s+" text))
(nil? (util/safe-re-find #"(?m)^\s*\*+\s+" text))
(nil? (util/safe-re-find #"(?:\r?\n){2,}" text))]
[:markdown false _ _]
(paste-text-parseable format text)

[:org _ false _]
(paste-text-parseable format text)

[:markdown true _ false]
(paste-segmented-text format text)

[:markdown true _ true]
(replace-text-f text)

[:org _ true false]
(paste-segmented-text format text)

[:org _ true true]
(replace-text-f text)))))))))

(defn paste-text-in-one-block-at-point
[]
Expand Down
18 changes: 8 additions & 10 deletions src/main/frontend/util.cljc
Expand Up @@ -770,16 +770,14 @@

#?(:cljs
(defn copy-to-clipboard!
([s]
(utils/writeClipboard (clj->js {:text s})))
([s html]
(utils/writeClipboard (clj->js {:text s :html html})))
([s html owner-window]
(-> (cond-> {:text s}
(not (string/blank? html))
(assoc :html html))
(bean/->js)
(utils/writeClipboard owner-window)))))
[text & {:keys [html blocks owner-window]}]
(let [data (clj->js
{:text text
:html html
:blocks (when blocks (pr-str blocks))})]
(if owner-window
(utils/writeClipboard data owner-window)
(utils/writeClipboard data)))))

(defn drop-nth [n coll]
(keep-indexed #(when (not= %1 n) %2) coll))
Expand Down
17 changes: 16 additions & 1 deletion src/main/frontend/utils.js
Expand Up @@ -249,7 +249,9 @@ export const getClipText = (cb, errorHandler) => {
})
}

export const writeClipboard = ({text, html}, ownerWindow) => {
export const writeClipboard = ({text, html, blocks}, ownerWindow) => {
console.log("block")
console.log(blocks)
if (Capacitor.isNativePlatform()) {
CapacitorClipboard.write({ string: text });
return
Expand Down Expand Up @@ -281,6 +283,19 @@ export const writeClipboard = ({text, html}, ownerWindow) => {
["text/html"]: richBlob
})];
}
if (blocks) {
let blocksBlob = new Blob([blocks], {
type: ["web application/logseq"]
})
let richBlob = new Blob([html], {
type: ["text/html"]
})
data = [new ClipboardItem({
["text/plain"]: blob,
["text/html"]: richBlob,
["web application/logseq"]: blocksBlob
})];
}
promise_written = navigator.clipboard.write(data)
} else {
console.debug("Degraded copy without `ClipboardItem` support:", text)
Expand Down

0 comments on commit bc635ea

Please sign in to comment.