Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get clipboard data in HTML format #2406

Merged
merged 2 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@
"react-intersection-observer": "^8.32.1",
"react-window": "^1.8.6",
"textarea-caret": "^3.1.0",
"tslib": "^2.3.1"
"tslib": "^2.3.1",
"turndown": "^7.1.1"
},
"devDependencies": {
"@babel/cli": "^7.15.4",
Expand Down
12 changes: 12 additions & 0 deletions src/cljs/athens/utils/markdown.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns athens.utils.markdown
(:require ["turndown" :as turndown]))

(set! (.-escape (.-prototype turndown)) (fn [string] string))

(defonce turndown-instance
(new turndown))

(defn html->md
"Transforms text to markdown."
[text]
(.turndown turndown-instance text))
11 changes: 9 additions & 2 deletions src/cljs/athens/views/blocks/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[athens.parse-renderer :refer [parse-and-render]]
[athens.subs.selection :as select-subs]
[athens.util :as util]
[athens.utils.markdown :as markdown]
[athens.views.blocks.autocomplete-search :as autocomplete-search]
[athens.views.blocks.autocomplete-slash :as autocomplete-slash]
[athens.views.blocks.internal-representation :as internal-representation]
Expand Down Expand Up @@ -106,7 +107,6 @@

;; Event Handlers


(defn textarea-paste
"Clipboard data can only be accessed if user triggers JavaScript paste event.
Uses previous keydown event to determine if shift was held, since the paste event has no knowledge of shift key.
Expand All @@ -128,7 +128,11 @@
:or {default-verbatim-paste? false}
:as _state-hooks} last-key-w-shift?]
(let [data (.. e -clipboardData)
md-data (-> data
(.getData "text/html")
(markdown/html->md))
text-data (.getData data "text/plain")

;; With internal representation
internal-representation (some-> (.getData data "application/athens-representation")
edn/read-string)
Expand All @@ -145,7 +149,10 @@
50))

;; External to internal representation
text-to-inter (when-not (str/blank? text-data)
text-to-inter (cond
(not (str/blank? md-data))
(internal-representation/text-to-internal-representation md-data)
(not (str/blank? text-data))
(internal-representation/text-to-internal-representation text-data))
line-breaks (re-find #"\r?\n" text-data)
no-shift (not @last-key-w-shift?)
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7292,6 +7292,11 @@ domhandler@^4.0.0, domhandler@^4.2.0:
dependencies:
domelementtype "^2.2.0"

domino@^2.1.6:
version "2.1.6"
resolved "https://registry.yarnpkg.com/domino/-/domino-2.1.6.tgz#fe4ace4310526e5e7b9d12c7de01b7f485a57ffe"
integrity sha512-3VdM/SXBZX2omc9JF9nOPCtDaYQ67BGp5CoLpIQlO2KCAPETs8TcDHacF26jXadGbvUteZzRTeos2fhID5+ucQ==

domutils@^2.5.2, domutils@^2.6.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/domutils/-/domutils-2.7.0.tgz#8ebaf0c41ebafcf55b0b72ec31c56323712c5442"
Expand Down Expand Up @@ -14256,6 +14261,13 @@ tunnel@^0.0.6:
resolved "https://registry.yarnpkg.com/tunnel/-/tunnel-0.0.6.tgz#72f1314b34a5b192db012324df2cc587ca47f92c"
integrity sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==

turndown@^7.1.1:
version "7.1.1"
resolved "https://registry.yarnpkg.com/turndown/-/turndown-7.1.1.tgz#96992f2d9b40a1a03d3ea61ad31b5a5c751ef77f"
integrity sha512-BEkXaWH7Wh7e9bd2QumhfAXk5g34+6QUmmWx+0q6ThaVOLuLUqsnkq35HQ5SBHSaxjSfSM7US5o4lhJNH7B9MA==
dependencies:
domino "^2.1.6"

type-check@~0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72"
Expand Down