├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── project.clj ├── resources ├── icons │ ├── icon_128.png │ └── icon_16.png └── manifest_template.json ├── src └── textarea_to_code_editor │ ├── background │ ├── chrome.cljs │ ├── core.cljs │ ├── handlers.cljs │ └── modes.cljs │ ├── content │ ├── chrome.cljs │ ├── core.cljs │ ├── editor.cljs │ └── handlers.cljs │ ├── core.clj │ └── macros.clj └── test └── textarea_to_code_editor ├── background ├── handlers_test.cljs └── modes_test.cljs └── content ├── editor_test.cljs └── handlers_test.cljs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .lein-deps-sum 11 | .lein-repl-history 12 | .lein-plugins/ 13 | resources/public/cljs-target/ 14 | hs_err* 15 | /resources/*.js 16 | /resources/manifest.json 17 | /resources/components 18 | /resources/background 19 | /resources/content 20 | .idea 21 | *iml 22 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: clojure 2 | lein: lein2 3 | jdk: 4 | - oraclejdk7 5 | before_install: 6 | - sudo add-apt-repository ppa:chris-lea/node.js -y 7 | - sudo apt-get update -qq 8 | - sudo apt-get install -qq phantomjs nodejs 9 | - sudo npm install -g bower 10 | install: 11 | - lein2 bower install 12 | script: 13 | - lein2 cljsbuild test 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # textarea-to-code-editor 2 | 3 | [](https://travis-ci.org/nvbn/textarea-to-code-editor) 4 | 5 | Chrome extension for converting textarea to code editor 6 | 7 | [](https://chrome.google.com/webstore/detail/kcapdaijpdnhajjgdimlhoaaaiplkobj) 8 | 9 | ## Building 10 | 11 | For building local version of extensions you should run: 12 | 13 | ```bash 14 | lein bower install 15 | lein cljsbuild once 16 | lein run 17 | ``` 18 | 19 | And install unpacked extension from `resources`. 20 | 21 | For running tests: 22 | 23 | ```bash 24 | lein cljsbuild test 25 | ``` 26 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject textarea-to-code-editor "0.5" 2 | :description "Chrome extension for converting textarea to code editor" 3 | :url "https://github.com/nvbn/textarea-to-code-editor" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.8.0"] 7 | [org.clojure/clojurescript "1.7.228"] 8 | [org.clojure/core.match "0.3.0-alpha4"] 9 | [org.clojure/core.async "0.2.371"] 10 | [com.cognitect/transit-cljs "0.8.237"] 11 | [com.cemerick/clojurescript.test "0.3.3"] 12 | [domina "1.0.3"] 13 | [clj-di "0.5.0"] 14 | [selmer "0.8.0"] 15 | [alandipert/storage-atom "1.2.4"] 16 | [org.clojure/tools.macro "0.1.2"]] 17 | :plugins [[lein-cljsbuild "1.1.3"] 18 | [com.cemerick/clojurescript.test "0.3.3"] 19 | [lein-bower "0.5.1"]] 20 | :bower-dependencies [[ace-builds "~1.2.3"]] 21 | :bower {:directory "resources/components/"} 22 | :jvm-opts ["-Xss16m"] 23 | :content-scripts ["content/main.js" 24 | "components/ace-builds/src/" 25 | "components/ace-builds/src/snippets/"] 26 | :background-scripts ["background/main.js"] 27 | :main textarea-to-code-editor.core 28 | :cljsbuild {:builds {:background {:source-paths ["src/textarea_to_code_editor/background/"] 29 | :compiler {:output-to "resources/background/main.js" 30 | :output-dir "resources/background/" 31 | :source-map "resources/background/main.js.map" 32 | :optimizations :whitespace 33 | :pretty-print true}} 34 | :content {:source-paths ["src/textarea_to_code_editor/content/"] 35 | :compiler {:output-to "resources/content/main.js" 36 | :output-dir "resources/content/" 37 | :source-map "resources/content/main.js.map" 38 | :optimizations :whitespace 39 | :pretty-print true}} 40 | :test {:source-paths ["src/" "test/"] 41 | :compiler {:output-to "target/cljs-test.js" 42 | :optimizations :whitespace 43 | :pretty-print false}}} 44 | :test-commands {"test" ["phantomjs" :runner 45 | "resources/components/ace-builds/src/ace.js" 46 | "resources/components/ace-builds/src/mode-clojure.js" 47 | "resources/components/ace-builds/src/mode-python.js" 48 | "resources/components/ace-builds/src/theme-monokai.js" 49 | "resources/components/ace-builds/src/ext-modelist.js" 50 | "target/cljs-test.js"]}}) 51 | -------------------------------------------------------------------------------- /resources/icons/icon_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvbn/textarea-to-code-editor/16ce92bb1d17eccc5f49c893b31491738f4ba264/resources/icons/icon_128.png -------------------------------------------------------------------------------- /resources/icons/icon_16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvbn/textarea-to-code-editor/16ce92bb1d17eccc5f49c893b31491738f4ba264/resources/icons/icon_16.png -------------------------------------------------------------------------------- /resources/manifest_template.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "{{ name }}", 3 | "description": "{{ description }}", 4 | "version": "{{ version }}", 5 | "permissions": [ 6 | "contextMenus", 7 | "tabs", 8 | "http://*/*", 9 | "https://*/*" 10 | ], 11 | "background": { 12 | "scripts": {{ background-scripts|json|safe }} 13 | }, 14 | "content_scripts": [ 15 | { 16 | "matches": ["*://*/*"], 17 | "js": {{ content-scripts|json|safe }}, 18 | "run_at": "document_end" 19 | } 20 | ], 21 | "web_accessible_resources": [ 22 | "content/*" 23 | ], 24 | "icons": { 25 | "16": "icons/icon_16.png", 26 | "128": "icons/icon_128.png" 27 | }, 28 | "manifest_version": 2 29 | } 30 | -------------------------------------------------------------------------------- /src/textarea_to_code_editor/background/chrome.cljs: -------------------------------------------------------------------------------- 1 | (ns textarea-to-code-editor.background.chrome 2 | (:require-macros [cljs.core.async.macros :refer [go-loop go]]) 3 | (:require [cljs.core.async :refer [! chan]] 4 | [cognitect.transit :as t])) 5 | 6 | (defn available? [] (aget js/window "chrome")) 7 | 8 | (defn get-sender-chan 9 | "Returns channel for sending response to tab." 10 | [sender] 11 | (let [tab-id (.. sender -tab -id) 12 | ch (chan)] 13 | (go-loop [] 14 | (.. js/chrome -tabs (sendMessage tab-id (t/write (t/writer :json) 15 | (! msg-chan (conj (t/read (t/reader :json) %1) 24 | (get-sender-chan %2))))))) 25 | 26 | (defn clear-context-menu! 27 | "Removes all create context menus." 28 | [] 29 | (.. js/chrome -contextMenus (removeAll))) 30 | 31 | (defn create-context-menu! 32 | "Creates new context menu." 33 | [menu] 34 | (.. js/chrome -contextMenus (create (clj->js menu)))) 35 | -------------------------------------------------------------------------------- /src/textarea_to_code_editor/background/core.cljs: -------------------------------------------------------------------------------- 1 | (ns textarea-to-code-editor.background.core 2 | (:require-macros [cljs.core.async.macros :refer [go-loop]]) 3 | (:require [cljs.core.match :refer-macros [match]] 4 | [cljs.core.async :refer [! chan]] 5 | [textarea-to-code-editor.background.chrome :as c] 6 | [textarea-to-code-editor.background.modes :as m])) 7 | 8 | (defn create-context-menus! 9 | "Creates context menus instances in chrome." 10 | [& menus] 11 | (doseq [menu (flatten menus)] 12 | (c/create-context-menu! (assoc menu 13 | :contexts [:all])))) 14 | 15 | (defn get-menus-for-modes 16 | "Returns params for context menus for passed modes" 17 | [modes current-mode parent-id sender-chan msg-chan] 18 | (for [mode modes] 19 | {:title (:caption mode) 20 | :parentId parent-id 21 | :type :checkbox 22 | :checked (= current-mode mode) 23 | :onclick #(go (>! sender-chan [:change-mode mode]) 24 | (>! msg-chan [:update-used-modes mode nil]))})) 25 | 26 | (defhandler populate-context-menu! 27 | "Shows context menu when mouse on textarea." 28 | [{:keys [current-mode modes]} used-modes sender-chan msg-chan] 29 | (c/clear-context-menu!) 30 | (create-context-menus! 31 | {:title "Textarea to code editor" 32 | :id :textarea-to-code-editor} 33 | {:title "Normal textarea" 34 | :parentId :textarea-to-code-editor 35 | :type :checkbox 36 | :checked (nil? current-mode) 37 | :onclick #(go (>! sender-chan [:change-mode :textarea]))} 38 | {:parentId :textarea-to-code-editor 39 | :type :separator} 40 | (get-menus-for-modes (m/sanitize-used-modes modes used-modes) 41 | current-mode :textarea-to-code-editor 42 | sender-chan msg-chan) 43 | {:title "More" 44 | :parentId :textarea-to-code-editor 45 | :id :textarea-to-editor-more} 46 | (get-menus-for-modes (m/get-unused-modes modes used-modes) 47 | current-mode :textarea-to-editor-more 48 | sender-chan msg-chan))) 49 | 50 | (defhandler clear-context-menu! 51 | "Clears context menu safely." 52 | [] 53 | (c/clear-context-menu!)) 54 | 55 | (defhandler update-used-modes! 56 | "Updates list of used modes in local storage." 57 | [storage mode] 58 | (swap! storage update-in [:used-modes] 59 | m/update-used-modes mode)) 60 | -------------------------------------------------------------------------------- /src/textarea_to_code_editor/background/modes.cljs: -------------------------------------------------------------------------------- 1 | (ns textarea-to-code-editor.background.modes) 2 | 3 | (def used-modes-limit 5) 4 | 5 | (def default-used-modes 6 | [{:caption "Python" 7 | :mode "ace/mode/python"} 8 | {:caption "SH" 9 | :mode "ace/mode/sh"} 10 | {:caption "JavaScript" 11 | :mode "ace/mode/javascript"} 12 | {:caption "HTML" 13 | :mode "ace/mode/html"} 14 | {:caption "Markdown" 15 | :mode "ace/mode/markdown"}]) 16 | 17 | (defn sanitize-used-modes 18 | "Returns list of valid used modes." 19 | [modes used-modes] 20 | (filter (set modes) used-modes)) 21 | 22 | (defn update-used-modes 23 | "Update list of used modes." 24 | [used-modes mode] 25 | (->> used-modes 26 | (remove #(= mode %)) 27 | (into [mode]) 28 | (take used-modes-limit))) 29 | 30 | (defn get-unused-modes 31 | "Returns list with ordered unused modes." 32 | [modes used-modes] 33 | (->> modes 34 | (filter (complement (set used-modes))) 35 | (sort-by :caption))) 36 | -------------------------------------------------------------------------------- /src/textarea_to_code_editor/content/chrome.cljs: -------------------------------------------------------------------------------- 1 | (ns textarea-to-code-editor.content.chrome 2 | (:require-macros [cljs.core.async.macros :refer [go-loop go]]) 3 | (:require [cljs.core.async :refer [! chan]] 4 | [cognitect.transit :as t])) 5 | 6 | (defn available? [] (aget js/window "chrome")) 7 | 8 | (defn subscribe-to-runtime! 9 | "Puts all runtime messages to msg-chan." 10 | [msg-chan] 11 | (.. js/chrome -runtime -onMessage 12 | (addListener #(go (>! msg-chan 13 | (conj (t/read (t/reader :json) %1) 14 | %2)))))) 15 | 16 | (defn get-runtime-chan 17 | "Returns channel for send message to runtime." 18 | [] 19 | (let [ch (chan)] 20 | (go-loop [] 21 | (.. js/chrome -runtime 22 | (sendMessage (t/write (t/writer :json) (! ! ch [:enter-editor (current-target %) nil]))) 16 | (listen! :mouseleave #(go (! ch [:leave-editor nil nil]))))) 18 | 19 | (defn init-editor! 20 | "Initializes text editor." 21 | [textarea editor-el {:keys [mode]}] 22 | (let [editor (.edit js/ace editor-el)] 23 | (doto editor 24 | (.setTheme "ace/theme/monokai") 25 | (.setValue (value textarea)) 26 | (.. getSession (setMode mode)) 27 | (.. getSession (on "change" #(set-value! textarea (.getValue editor))))))) 28 | 29 | (defn div-from-textarea! 30 | "Creates div from textarea." 31 | [textarea] 32 | (let [id (str (gensym))] 33 | (doto textarea 34 | (insert-before! (str "
")) 39 | (set-attr! :data-editor-id id) 40 | (set-styles! {:display "none"})) 41 | (by-id id))) 42 | 43 | (defn to-code-editor! 44 | "Converts textarea to code editor." 45 | [el mode hover-chan] 46 | (let [editor-el (div-from-textarea! el)] 47 | (subscribe-to-hover! editor-el hover-chan) 48 | (init-editor! el editor-el mode))) 49 | 50 | (defn is-editor? 51 | "Returns true when element is editor." 52 | [el] 53 | (= (.-tagName el) "DIV")) 54 | 55 | (defn get-modes 56 | "Returns all available editor modes." 57 | [] 58 | (let [ace-modes (.. js/ace (require "ace/ext/modelist") -modes)] 59 | (for [mode ace-modes] 60 | {:caption (.-caption mode) 61 | :mode (.-mode mode)}))) 62 | 63 | (defn get-editor-mode 64 | "Returns current editor mode." 65 | [el] 66 | (when (is-editor? el) 67 | (let [mode-id (.. js/ace (edit el) getSession getMode -$id)] 68 | (first (filter #(= (:mode %) mode-id) (get-modes)))))) 69 | 70 | (defn change-editor-mode! 71 | "Changes editor mode." 72 | [el {:keys [mode]}] 73 | (.. js/ace (edit el) getSession (setMode mode)) 74 | el) 75 | 76 | (defn to-textarea! 77 | "Converts code editor back to textarea." 78 | [el] 79 | (set-styles! (sel (str "[data-editor-id=" (attr el :id) "]")) 80 | {:display "block"}) 81 | (destroy! el)) 82 | -------------------------------------------------------------------------------- /src/textarea_to_code_editor/content/handlers.cljs: -------------------------------------------------------------------------------- 1 | (ns textarea-to-code-editor.content.handlers 2 | (:require-macros [cljs.core.async.macros :refer [go]] 3 | [textarea-to-code-editor.macros :refer [defhandler]]) 4 | (:require [cljs.core.match :refer-macros [match]] 5 | [cljs.core.async :refer [>!]] 6 | [textarea-to-code-editor.content.editor :as e])) 7 | 8 | (defhandler change-mode! 9 | "Changes editor mode." 10 | [el mode hover-chan] 11 | (when el 12 | (match [(e/is-editor? el) mode] 13 | [true :textarea] (e/to-textarea! el) 14 | [false _] (e/to-code-editor! el mode hover-chan) 15 | [true _] (e/change-editor-mode! el mode)))) 16 | 17 | (defhandler populate-context-menu! 18 | "Populates context menu with available modes." 19 | [el runtime-chan] 20 | (go (>! runtime-chan [:populate-context-menu 21 | {:current-mode (e/get-editor-mode el) 22 | :modes (e/get-modes)}])) 23 | el) 24 | 25 | (defhandler clear-context-menu! 26 | "Clears context menu." 27 | [runtime-chan] 28 | (go (>! runtime-chan [:clear-context-menu nil])) 29 | nil) 30 | -------------------------------------------------------------------------------- /src/textarea_to_code_editor/core.clj: -------------------------------------------------------------------------------- 1 | (ns textarea-to-code-editor.core 2 | (:require [clojure.string :as string] 3 | [clojure.java.io :as io] 4 | [selmer.parser :refer [render-file]])) 5 | 6 | (defn list-resources 7 | "Returns list with resources from paths." 8 | [paths] 9 | (->> (for [script paths 10 | :let [file (io/file (io/resource script))]] 11 | (if (.isDirectory file) 12 | (map #(string/replace (.getPath %) #"^.*resources/" "") 13 | (.listFiles file)) 14 | [script])) 15 | flatten 16 | sort 17 | (filter #(-> % io/resource io/file .isDirectory not)))) 18 | 19 | (defn -main 20 | "Generates new manifest.json on lein run" 21 | [& _] 22 | (let [project-data (read-string (slurp "project.clj")) 23 | [_ project-name project-version & _] project-data 24 | project-name (string/replace (str project-name) #"-" " ") 25 | project-map (apply hash-map (drop 3 project-data)) 26 | content-scripts (list-resources (:content-scripts project-map)) 27 | background-scripts (list-resources (:background-scripts project-map))] 28 | (with-open [wrtr (io/writer "resources/manifest.json")] 29 | (.write wrtr (render-file "manifest_template.json" 30 | {:name project-name 31 | :version project-version 32 | :description (:description project-map) 33 | :content-scripts content-scripts 34 | :background-scripts background-scripts})))) 35 | (println "Manifest updated!")) 36 | -------------------------------------------------------------------------------- /src/textarea_to_code_editor/macros.clj: -------------------------------------------------------------------------------- 1 | (ns textarea-to-code-editor.macros 2 | (:require [clojure.tools.macro :refer [name-with-attributes]])) 3 | 4 | (defmacro defhandler 5 | "Defines messages handler which not fails on errors." 6 | [name & body] 7 | (let [[name [args & body]] (name-with-attributes name body)] 8 | `(defn ~name 9 | ~args 10 | (try (do ~@body) 11 | (catch :default e# 12 | (println e#)))))) 13 | -------------------------------------------------------------------------------- /test/textarea_to_code_editor/background/handlers_test.cljs: -------------------------------------------------------------------------------- 1 | (ns textarea-to-code-editor.background.handlers-test 2 | (:require-macros [cljs.core.async.macros :refer [go]] 3 | [clj-di.test :refer [with-reset]]) 4 | (:require [cemerick.cljs.test :refer-macros [deftest is testing done]] 5 | [cljs.core.async :refer [ `used-modes-limit`" 42 | (with-redefs [m/used-modes-limit 1] 43 | (is (= (m/update-used-modes [{:caption "Python" 44 | :mode "ace/mode/python"}] 45 | {:caption "HTML" 46 | :mode "ace/mode/html"}) 47 | [{:caption "HTML" 48 | :mode "ace/mode/html"}]))))) 49 | 50 | (deftest test-get-unused-modes 51 | (testing "Should return only unused modes" 52 | (is (= (m/get-unused-modes [{:caption "Python" 53 | :mode "ace/mode/python"} 54 | {:caption "HTML" 55 | :mode "ace/mode/html"}] 56 | [{:caption "HTML" 57 | :mode "ace/mode/html"}]) 58 | [{:caption "Python" 59 | :mode "ace/mode/python"}])))) 60 | -------------------------------------------------------------------------------- /test/textarea_to_code_editor/content/editor_test.cljs: -------------------------------------------------------------------------------- 1 | (ns textarea-to-code-editor.content.editor-test 2 | (:require-macros [cljs.core.async.macros :refer [go]]) 3 | (:require [cemerick.cljs.test :refer-macros [deftest testing is done use-fixtures]] 4 | [cljs.core.async :refer [! chan]] 5 | [domina.css :refer [sel]] 6 | [domina.events :refer [dispatch! root-element is-event-target?]] 7 | [domina :refer [append! styles attr has-class? classes 8 | destroy-children! by-id nodes value]] 9 | [textarea-to-code-editor.content.editor :as e])) 10 | 11 | (use-fixtures :each 12 | (fn [f] 13 | (f) 14 | (destroy-children! (sel "body")))) 15 | 16 | (def mode {:caption "Clojure" 17 | :mode "ace/mode/clojure"}) 18 | 19 | (def py-mode {:caption "Python" 20 | :mode "ace/mode/python"}) 21 | 22 | (defn add-el-with-random-id! 23 | ([el] (add-el-with-random-id! el "")) 24 | ([el content] 25 | (let [el-id (str (gensym)) 26 | el-name (clj->js el)] 27 | (append! (sel "body") (str "<" el-name " id='" el-id "'>" content "" el-name ">")) 28 | (by-id el-id)))) 29 | 30 | (deftest ^:async test-subscribe-to-hover! 31 | (go (let [el (add-el-with-random-id! :textarea "test") 32 | ch (chan)] 33 | (e/subscribe-to-hover! el ch) 34 | (testing "On mouse enter" 35 | (dispatch! el :mouseenter {}) 36 | (is (= (first ( (e/get-modes) first :mode)) 78 | (is (-> (e/get-modes) first :caption)))) 79 | 80 | (deftest test-get-editor-mode 81 | (testing "nil for not editor" 82 | (let [textarea (add-el-with-random-id! :textarea)] 83 | (is (nil? (e/get-editor-mode textarea))))) 84 | (testing "current mode for editor" 85 | (let [textarea (add-el-with-random-id! :textarea) 86 | div (add-el-with-random-id! :div)] 87 | (e/init-editor! textarea div mode) 88 | (is (= (e/get-editor-mode div) mode))))) 89 | 90 | (deftest test-change-editor-mode! 91 | (testing "Change editor mode" 92 | (let [textarea (add-el-with-random-id! :textarea) 93 | div (add-el-with-random-id! :div)] 94 | (e/init-editor! textarea div mode) 95 | (e/change-editor-mode! div py-mode) 96 | (is (= (e/get-editor-mode div) py-mode))))) 97 | 98 | (deftest test-to-textarea! 99 | (let [textarea (add-el-with-random-id! :textarea) 100 | div (add-el-with-random-id! :div)] 101 | (e/init-editor! textarea div mode) 102 | (e/to-textarea! div) 103 | (testing "Div should be removed" 104 | (is (-> (sel "div") nodes count zero?))))) 105 | -------------------------------------------------------------------------------- /test/textarea_to_code_editor/content/handlers_test.cljs: -------------------------------------------------------------------------------- 1 | (ns textarea-to-code-editor.content.handlers-test 2 | (:require-macros [cljs.core.async.macros :refer [go]] 3 | [clj-di.test :refer [with-reset]]) 4 | (:require [cemerick.cljs.test :refer-macros [deftest testing is done]] 5 | [cljs.core.async :refer [