kiteditor 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (229) hide show
  1. data/LICENSE +25 -0
  2. data/app/controllers/mercury/images_controller.rb +5 -0
  3. data/app/controllers/mercury_controller.rb +34 -0
  4. data/app/models/mercury/image.rb +17 -0
  5. data/app/views/layouts/mercury.html.erb +33 -0
  6. data/app/views/layouts/popup.html.haml +9 -0
  7. data/app/views/mercury/images/_list.html.haml +22 -0
  8. data/app/views/mercury/images/index.html.haml +4 -0
  9. data/app/views/mercury/images/index.js.erb +2 -0
  10. data/app/views/mercury/lightviews/about.html +11 -0
  11. data/app/views/mercury/modals/character.html +255 -0
  12. data/app/views/mercury/modals/htmleditor.html +13 -0
  13. data/app/views/mercury/modals/link.html +94 -0
  14. data/app/views/mercury/modals/media.html +1 -0
  15. data/app/views/mercury/modals/table.html +84 -0
  16. data/app/views/mercury/palettes/backcolor.html +73 -0
  17. data/app/views/mercury/palettes/forecolor.html +73 -0
  18. data/app/views/mercury/panels/history.html +3 -0
  19. data/app/views/mercury/panels/notes.html +3 -0
  20. data/app/views/mercury/panels/snippets.html +12 -0
  21. data/app/views/mercury/selects/formatblock.html +11 -0
  22. data/app/views/mercury/selects/style.html +5 -0
  23. data/app/views/mercury/snippets/example/options.html.erb +34 -0
  24. data/app/views/mercury/snippets/example/preview.html.erb +1 -0
  25. data/config/engine.rb +44 -0
  26. data/db/migrate/20110526035601_create_mercury_images.rb +11 -0
  27. data/features/loading/loading.feature +22 -0
  28. data/features/loading/navigating.feature +77 -0
  29. data/features/loading/user_interface.feature +67 -0
  30. data/features/regions/editable/advanced_editing.feature +0 -0
  31. data/features/regions/editable/basic_editing.feature +195 -0
  32. data/features/regions/editable/inserting_links.feature +98 -0
  33. data/features/regions/editable/inserting_media.feature +110 -0
  34. data/features/regions/editable/inserting_snippets.feature +102 -0
  35. data/features/regions/editable/inserting_special_characters.feature +24 -0
  36. data/features/regions/editable/inserting_tables.feature +109 -0
  37. data/features/regions/editable/pasting.feature +0 -0
  38. data/features/regions/editable/uploading_images.feature +0 -0
  39. data/features/regions/markupable/advanced_editing.feature +0 -0
  40. data/features/regions/markupable/basic_editing.feature +0 -0
  41. data/features/regions/markupable/inserting_links.feature +0 -0
  42. data/features/regions/markupable/inserting_media.feature +0 -0
  43. data/features/regions/markupable/inserting_snippets.feature +0 -0
  44. data/features/regions/markupable/inserting_special_characters.feature +0 -0
  45. data/features/regions/markupable/inserting_tables.feature +0 -0
  46. data/features/regions/markupable/uploading_images.feature +0 -0
  47. data/features/regions/snippetable/advanced_editing.feature +0 -0
  48. data/features/regions/snippetable/basic_editing.feature +0 -0
  49. data/features/regions/snippetable/inserting_snippets.feature +0 -0
  50. data/features/saving/saving.feature +33 -0
  51. data/features/step_definitions/debug_steps.rb +14 -0
  52. data/features/step_definitions/mercury_steps.rb +438 -0
  53. data/features/step_definitions/web_steps.rb +211 -0
  54. data/features/support/env.rb +46 -0
  55. data/features/support/mercury_contents.rb +25 -0
  56. data/features/support/mercury_selectors.rb +148 -0
  57. data/features/support/paths.rb +38 -0
  58. data/features/support/selectors.rb +44 -0
  59. data/lib/generators/mercury/install/install_generator.rb +49 -0
  60. data/lib/generators/mercury/install/templates/mongoid_paperclip_image.rb +17 -0
  61. data/lib/mercury/authentication.rb +8 -0
  62. data/lib/mercury-rails.rb +3 -0
  63. data/spec/javascripts/mercury/dialog_spec.js.coffee +281 -0
  64. data/spec/javascripts/mercury/dialogs/backcolor_spec.js.coffee +37 -0
  65. data/spec/javascripts/mercury/dialogs/forecolor_spec.js.coffee +37 -0
  66. data/spec/javascripts/mercury/dialogs/formatblock_spec.js.coffee +25 -0
  67. data/spec/javascripts/mercury/dialogs/snippetpanel_spec.js.coffee +30 -0
  68. data/spec/javascripts/mercury/dialogs/style_spec.js.coffee +25 -0
  69. data/spec/javascripts/mercury/history_buffer_spec.js.coffee +76 -0
  70. data/spec/javascripts/mercury/lightview_spec.js.coffee +497 -0
  71. data/spec/javascripts/mercury/mercury_spec.js.coffee +132 -0
  72. data/spec/javascripts/mercury/modal_spec.js.coffee +504 -0
  73. data/spec/javascripts/mercury/modals/htmleditor_spec.js.coffee +30 -0
  74. data/spec/javascripts/mercury/modals/insertcharacter_spec.js.coffee +29 -0
  75. data/spec/javascripts/mercury/modals/insertlink_spec.js.coffee +220 -0
  76. data/spec/javascripts/mercury/modals/insertmedia_spec.js.coffee +167 -0
  77. data/spec/javascripts/mercury/modals/insertsnippet_spec.js.coffee +52 -0
  78. data/spec/javascripts/mercury/modals/inserttable_spec.js.coffee +160 -0
  79. data/spec/javascripts/mercury/native_extensions_spec.js.coffee +60 -0
  80. data/spec/javascripts/mercury/page_editor_spec.js.coffee +750 -0
  81. data/spec/javascripts/mercury/palette_spec.js.coffee +49 -0
  82. data/spec/javascripts/mercury/panel_spec.js.coffee +185 -0
  83. data/spec/javascripts/mercury/region_spec.js.coffee +298 -0
  84. data/spec/javascripts/mercury/regions/editable_spec.js.coffee +561 -0
  85. data/spec/javascripts/mercury/regions/markupable_spec.js.coffee +367 -0
  86. data/spec/javascripts/mercury/regions/snippetable_spec.js.coffee +370 -0
  87. data/spec/javascripts/mercury/select_spec.js.coffee +49 -0
  88. data/spec/javascripts/mercury/snippet_spec.js.coffee +245 -0
  89. data/spec/javascripts/mercury/snippet_toolbar_spec.js.coffee +184 -0
  90. data/spec/javascripts/mercury/statusbar_spec.js.coffee +150 -0
  91. data/spec/javascripts/mercury/table_editor_spec.js.coffee +194 -0
  92. data/spec/javascripts/mercury/toolbar.button_group_spec.js.coffee +90 -0
  93. data/spec/javascripts/mercury/toolbar.button_spec.js.coffee +360 -0
  94. data/spec/javascripts/mercury/toolbar.expander_spec.js.coffee +118 -0
  95. data/spec/javascripts/mercury/toolbar_spec.js.coffee +222 -0
  96. data/spec/javascripts/mercury/tooltip_spec.js.coffee +186 -0
  97. data/spec/javascripts/mercury/uploader_spec.js.coffee +514 -0
  98. data/spec/javascripts/spec_helper.js +513 -0
  99. data/spec/javascripts/templates/mercury/dialog.html +2 -0
  100. data/spec/javascripts/templates/mercury/dialogs/backcolor.html +5 -0
  101. data/spec/javascripts/templates/mercury/dialogs/forecolor.html +5 -0
  102. data/spec/javascripts/templates/mercury/dialogs/formatblock.html +3 -0
  103. data/spec/javascripts/templates/mercury/dialogs/snippetpanel.html +16 -0
  104. data/spec/javascripts/templates/mercury/dialogs/style.html +3 -0
  105. data/spec/javascripts/templates/mercury/lightview.html +13 -0
  106. data/spec/javascripts/templates/mercury/modal.html +13 -0
  107. data/spec/javascripts/templates/mercury/modals/htmleditor.html +5 -0
  108. data/spec/javascripts/templates/mercury/modals/insertcharacter.html +5 -0
  109. data/spec/javascripts/templates/mercury/modals/insertlink.html +30 -0
  110. data/spec/javascripts/templates/mercury/modals/insertmedia.html +35 -0
  111. data/spec/javascripts/templates/mercury/modals/insertsnippet.html +6 -0
  112. data/spec/javascripts/templates/mercury/modals/inserttable.html +27 -0
  113. data/spec/javascripts/templates/mercury/page_editor.html +35 -0
  114. data/spec/javascripts/templates/mercury/palette.html +16 -0
  115. data/spec/javascripts/templates/mercury/panel.html +16 -0
  116. data/spec/javascripts/templates/mercury/region.html +2 -0
  117. data/spec/javascripts/templates/mercury/regions/editable.html +3 -0
  118. data/spec/javascripts/templates/mercury/regions/snippetable.html +4 -0
  119. data/spec/javascripts/templates/mercury/select.html +16 -0
  120. data/spec/javascripts/templates/mercury/snippet.html +1 -0
  121. data/spec/javascripts/templates/mercury/snippet_toolbar.html +16 -0
  122. data/spec/javascripts/templates/mercury/statusbar.html +8 -0
  123. data/spec/javascripts/templates/mercury/table_editor.html +65 -0
  124. data/spec/javascripts/templates/mercury/toolbar.button.html +64 -0
  125. data/spec/javascripts/templates/mercury/toolbar.button_group.html +9 -0
  126. data/spec/javascripts/templates/mercury/toolbar.expander.html +18 -0
  127. data/spec/javascripts/templates/mercury/toolbar.html +11 -0
  128. data/spec/javascripts/templates/mercury/tooltip.html +12 -0
  129. data/spec/javascripts/templates/mercury/uploader.html +11 -0
  130. data/vendor/assets/images/mercury/button.png +0 -0
  131. data/vendor/assets/images/mercury/close.png +0 -0
  132. data/vendor/assets/images/mercury/default-snippet.png +0 -0
  133. data/vendor/assets/images/mercury/loading-dark.gif +0 -0
  134. data/vendor/assets/images/mercury/loading-light.gif +0 -0
  135. data/vendor/assets/images/mercury/missing-image.png +0 -0
  136. data/vendor/assets/images/mercury/search-icon.png +0 -0
  137. data/vendor/assets/images/mercury/temp-logo.png +0 -0
  138. data/vendor/assets/images/mercury/toolbar/editable/buttons.png +0 -0
  139. data/vendor/assets/images/mercury/toolbar/primary/_expander.png +0 -0
  140. data/vendor/assets/images/mercury/toolbar/primary/_pressed.png +0 -0
  141. data/vendor/assets/images/mercury/toolbar/primary/historypanel.png +0 -0
  142. data/vendor/assets/images/mercury/toolbar/primary/insertcharacter.png +0 -0
  143. data/vendor/assets/images/mercury/toolbar/primary/insertlink.png +0 -0
  144. data/vendor/assets/images/mercury/toolbar/primary/insertmedia.png +0 -0
  145. data/vendor/assets/images/mercury/toolbar/primary/inserttable.png +0 -0
  146. data/vendor/assets/images/mercury/toolbar/primary/inspectorpanel.png +0 -0
  147. data/vendor/assets/images/mercury/toolbar/primary/notespanel.png +0 -0
  148. data/vendor/assets/images/mercury/toolbar/primary/preview.png +0 -0
  149. data/vendor/assets/images/mercury/toolbar/primary/redo.png +0 -0
  150. data/vendor/assets/images/mercury/toolbar/primary/save.png +0 -0
  151. data/vendor/assets/images/mercury/toolbar/primary/snippetpanel.png +0 -0
  152. data/vendor/assets/images/mercury/toolbar/primary/undo.png +0 -0
  153. data/vendor/assets/images/mercury/toolbar/snippetable/buttons.png +0 -0
  154. data/vendor/assets/javascripts/mercury/dependencies/jquery-1.7.js +9300 -0
  155. data/vendor/assets/javascripts/mercury/dependencies/jquery-ui-1.8.13.custom.js +1328 -0
  156. data/vendor/assets/javascripts/mercury/dependencies/jquery-ui-1.8.18.custom.min.js +356 -0
  157. data/vendor/assets/javascripts/mercury/dependencies/jquery-ui.1.8.13.custom.min.js +356 -0
  158. data/vendor/assets/javascripts/mercury/dependencies/jquery.additions.js +206 -0
  159. data/vendor/assets/javascripts/mercury/dependencies/jquery.htmlClean.js +527 -0
  160. data/vendor/assets/javascripts/mercury/dependencies/liquidmetal.js +88 -0
  161. data/vendor/assets/javascripts/mercury/dependencies/showdown.js +1340 -0
  162. data/vendor/assets/javascripts/mercury/dialog.js.coffee +159 -0
  163. data/vendor/assets/javascripts/mercury/dialogs/backcolor.js.coffee +6 -0
  164. data/vendor/assets/javascripts/mercury/dialogs/forecolor.js.coffee +6 -0
  165. data/vendor/assets/javascripts/mercury/dialogs/formatblock.js.coffee +4 -0
  166. data/vendor/assets/javascripts/mercury/dialogs/snippetpanel.js.coffee +10 -0
  167. data/vendor/assets/javascripts/mercury/dialogs/style.js.coffee +4 -0
  168. data/vendor/assets/javascripts/mercury/finalize.js.coffee +3 -0
  169. data/vendor/assets/javascripts/mercury/history_buffer.js.coffee +30 -0
  170. data/vendor/assets/javascripts/mercury/lightview.js.coffee +205 -0
  171. data/vendor/assets/javascripts/mercury/locales/da.locale.js.coffee +0 -0
  172. data/vendor/assets/javascripts/mercury/locales/de.locale.js.coffee +206 -0
  173. data/vendor/assets/javascripts/mercury/locales/es.locale.js.coffee +211 -0
  174. data/vendor/assets/javascripts/mercury/locales/example.local.js.coffee +211 -0
  175. data/vendor/assets/javascripts/mercury/locales/fr.locale.js.coffee +211 -0
  176. data/vendor/assets/javascripts/mercury/locales/it.locale.js.coffee +208 -0
  177. data/vendor/assets/javascripts/mercury/locales/ko.local.js.coffee +206 -0
  178. data/vendor/assets/javascripts/mercury/locales/nl.locale.js.coffee +206 -0
  179. data/vendor/assets/javascripts/mercury/locales/pt.locale.js.coffee +211 -0
  180. data/vendor/assets/javascripts/mercury/locales/sv.local.js.coffee +209 -0
  181. data/vendor/assets/javascripts/mercury/locales/swedish_chef.locale.js.coffee +213 -0
  182. data/vendor/assets/javascripts/mercury/mercury.js.coffee +109 -0
  183. data/vendor/assets/javascripts/mercury/modal.js.coffee +204 -0
  184. data/vendor/assets/javascripts/mercury/modals/htmleditor.js.coffee +11 -0
  185. data/vendor/assets/javascripts/mercury/modals/insertcharacter.js.coffee +4 -0
  186. data/vendor/assets/javascripts/mercury/modals/insertlink.js.coffee +95 -0
  187. data/vendor/assets/javascripts/mercury/modals/insertmedia.js.coffee +107 -0
  188. data/vendor/assets/javascripts/mercury/modals/insertsnippet.js.coffee +12 -0
  189. data/vendor/assets/javascripts/mercury/modals/inserttable.js.coffee +54 -0
  190. data/vendor/assets/javascripts/mercury/native_extensions.js.coffee +55 -0
  191. data/vendor/assets/javascripts/mercury/page_editor.js.coffee +241 -0
  192. data/vendor/assets/javascripts/mercury/palette.js.coffee +29 -0
  193. data/vendor/assets/javascripts/mercury/panel.js.coffee +115 -0
  194. data/vendor/assets/javascripts/mercury/plugins/save_as_xml/mercury/page_editor.js.coffee +28 -0
  195. data/vendor/assets/javascripts/mercury/plugins/save_as_xml/plugin.js +9 -0
  196. data/vendor/assets/javascripts/mercury/region.js.coffee +107 -0
  197. data/vendor/assets/javascripts/mercury/regions/editable.js.coffee +600 -0
  198. data/vendor/assets/javascripts/mercury/regions/markupable.js.coffee +398 -0
  199. data/vendor/assets/javascripts/mercury/regions/simple.js.coffee +339 -0
  200. data/vendor/assets/javascripts/mercury/regions/snippetable.js.coffee +124 -0
  201. data/vendor/assets/javascripts/mercury/select.js.coffee +44 -0
  202. data/vendor/assets/javascripts/mercury/snippet.js.coffee +104 -0
  203. data/vendor/assets/javascripts/mercury/snippet_toolbar.js.coffee +72 -0
  204. data/vendor/assets/javascripts/mercury/statusbar.js.coffee +51 -0
  205. data/vendor/assets/javascripts/mercury/support/history.js +1 -0
  206. data/vendor/assets/javascripts/mercury/table_editor.js.coffee +265 -0
  207. data/vendor/assets/javascripts/mercury/toolbar.button.js.coffee +173 -0
  208. data/vendor/assets/javascripts/mercury/toolbar.button_group.js.coffee +42 -0
  209. data/vendor/assets/javascripts/mercury/toolbar.expander.js.coffee +56 -0
  210. data/vendor/assets/javascripts/mercury/toolbar.js.coffee +86 -0
  211. data/vendor/assets/javascripts/mercury/tooltip.js.coffee +74 -0
  212. data/vendor/assets/javascripts/mercury/uploader.js.coffee +227 -0
  213. data/vendor/assets/javascripts/mercury.js +479 -0
  214. data/vendor/assets/javascripts/mercury_loader.js +193 -0
  215. data/vendor/assets/javascripts/mercury_overrides.js +6 -0
  216. data/vendor/assets/javascripts/popup.js +8 -0
  217. data/vendor/assets/stylesheets/mercury/all_images.css.erb +89 -0
  218. data/vendor/assets/stylesheets/mercury/dialog.css +208 -0
  219. data/vendor/assets/stylesheets/mercury/lightview.css +151 -0
  220. data/vendor/assets/stylesheets/mercury/mercury.css +151 -0
  221. data/vendor/assets/stylesheets/mercury/modal.css +183 -0
  222. data/vendor/assets/stylesheets/mercury/statusbar.css +32 -0
  223. data/vendor/assets/stylesheets/mercury/toolbar.css +304 -0
  224. data/vendor/assets/stylesheets/mercury/tooltip.css +26 -0
  225. data/vendor/assets/stylesheets/mercury/uploader.css +111 -0
  226. data/vendor/assets/stylesheets/mercury.css +28 -0
  227. data/vendor/assets/stylesheets/mercury_overrides.css +17 -0
  228. data/vendor/assets/stylesheets/popup.css.erb +37 -0
  229. metadata +634 -0
@@ -0,0 +1,398 @@
1
+ # todo:
2
+ # context for the toolbar buttons and groups needs to change so we can do the following:
3
+ # how to handle context for buttons? if the cursor is within a bold area (**bo|ld**), or selecting it -- it would be
4
+ # nice if we could activate the bold button for instance.
5
+
6
+ class @Mercury.Regions.Markupable extends Mercury.Region
7
+ @supported: document.getElementById
8
+ @supportedText: "IE 7+, Chrome 10+, Firefox 4+, Safari 5+, Opera 8+"
9
+
10
+ type = 'markupable'
11
+
12
+ constructor: (@element, @window, @options = {}) ->
13
+ @type = 'markupable'
14
+ super
15
+ @converter = new Showdown.converter()
16
+
17
+
18
+ build: ->
19
+ width = '100%'
20
+ height = @element.height()
21
+
22
+ value = @element.html().replace(/^\s+|\s+$/g, '').replace('>', '>')
23
+ @element.removeClass(Mercury.config.regions.className)
24
+ @textarea = jQuery('<textarea>', @document).val(value)
25
+ @textarea.attr('class', @element.attr('class')).addClass('mercury-textarea')
26
+ @textarea.css({border: 0, background: 'transparent', display: 'block', 'overflow-y': 'hidden', width: width, height: height, fontFamily: '"Courier New", Courier, monospace'})
27
+ @element.addClass(Mercury.config.regions.className)
28
+ @element.empty().append(@textarea)
29
+
30
+ @previewElement = jQuery('<div>', @document)
31
+ @element.append(@previewElement)
32
+ @container = @element
33
+ @container.data('region', @)
34
+ @element = @textarea
35
+ @resize()
36
+
37
+
38
+ bindEvents: ->
39
+ Mercury.on 'mode', (event, options) => @togglePreview() if options.mode == 'preview'
40
+ Mercury.on 'focus:frame', => @focus() if !@previewing && Mercury.region == @
41
+
42
+ Mercury.on 'action', (event, options) =>
43
+ return if @previewing || Mercury.region != @
44
+ @execCommand(options.action, options) if options.action
45
+
46
+ Mercury.on 'unfocus:regions', =>
47
+ return if @previewing || Mercury.region != @
48
+ @element.blur()
49
+ @container.removeClass('focus')
50
+ Mercury.trigger('region:blurred', {region: @})
51
+
52
+ @element.on 'dragenter', (event) =>
53
+ return if @previewing
54
+ event.preventDefault()
55
+ event.originalEvent.dataTransfer.dropEffect = 'copy'
56
+
57
+ @element.on 'dragover', (event) =>
58
+ return if @previewing
59
+ event.preventDefault()
60
+ event.originalEvent.dataTransfer.dropEffect = 'copy'
61
+
62
+ @element.on 'drop', (event) =>
63
+ return if @previewing
64
+
65
+ # handle dropping snippets
66
+ if Mercury.snippet
67
+ event.preventDefault()
68
+ @focus()
69
+ Mercury.Snippet.displayOptionsFor(Mercury.snippet)
70
+
71
+ # handle any files that were dropped
72
+ if event.originalEvent.dataTransfer.files.length
73
+ event.preventDefault()
74
+ @focus()
75
+ Mercury.uploader(event.originalEvent.dataTransfer.files[0])
76
+
77
+ @element.on 'focus', =>
78
+ return if @previewing
79
+ Mercury.region = @
80
+ @container.addClass('focus')
81
+ Mercury.trigger('region:focused', {region: @})
82
+
83
+ @element.on 'keydown', (event) =>
84
+ return if @previewing
85
+ @resize()
86
+ switch event.keyCode
87
+ when 90 # undo / redo
88
+ return unless event.metaKey
89
+ event.preventDefault()
90
+ if event.shiftKey then @execCommand('redo') else @execCommand('undo')
91
+ return
92
+
93
+ when 13 # enter or return
94
+ selection = @selection()
95
+ text = @element.val()
96
+ start = text.lastIndexOf('\n', selection.start)
97
+ end = text.indexOf('\n', selection.end)
98
+ end = text.length if end < start
99
+ start = text.lastIndexOf('\n', selection.start - 1) if text[start] == '\n'
100
+ if text[start + 1] == '-'
101
+ selection.replace('\n- ', false, true)
102
+ event.preventDefault()
103
+ if /\d/.test(text[start + 1])
104
+ lineText = text.substring(start, end)
105
+ if /(\d+)\./.test(lineText)
106
+ number = parseInt(RegExp.$1)
107
+ selection.replace("\n#{number += 1}. ", false, true)
108
+ event.preventDefault()
109
+
110
+ when 9 # tab
111
+ event.preventDefault()
112
+ @execCommand('insertHTML', {value: ' '})
113
+
114
+ if event.metaKey
115
+ switch event.keyCode
116
+ when 66 # b
117
+ @execCommand('bold')
118
+ event.preventDefault()
119
+
120
+ when 73 # i
121
+ @execCommand('italic')
122
+ event.preventDefault()
123
+
124
+ when 85 # u
125
+ @execCommand('underline')
126
+ event.preventDefault()
127
+
128
+ @pushHistory(event.keyCode)
129
+
130
+ @element.on 'keyup', =>
131
+ return if @previewing
132
+ Mercury.changes = true
133
+ @resize()
134
+ Mercury.trigger('region:update', {region: @})
135
+
136
+ @element.on 'mouseup', =>
137
+ return if @previewing
138
+ @focus()
139
+ Mercury.trigger('region:focused', {region: @})
140
+
141
+ @previewElement.on 'click', (event) =>
142
+ $(event.target).closest('a').attr('target', '_parent') if @previewing
143
+
144
+
145
+ focus: ->
146
+ @element.focus()
147
+
148
+
149
+ content: (value = null, filterSnippets = true) ->
150
+ if value != null
151
+ if jQuery.type(value) == 'string'
152
+ @element.val(value)
153
+ else
154
+ @element.val(value.html)
155
+ @selection().select(value.selection.start, value.selection.end)
156
+ else
157
+ return @element.val()
158
+
159
+
160
+ contentAndSelection: ->
161
+ return {html: @content(null, false), selection: @selection().serialize()}
162
+
163
+
164
+ togglePreview: ->
165
+ if @previewing
166
+ @previewing = false
167
+ @container.addClass(Mercury.config.regions.className).removeClass("#{Mercury.config.regions.className}-preview")
168
+ @previewElement.hide()
169
+ @element.show()
170
+ @focus() if Mercury.region == @
171
+ else
172
+ @previewing = true
173
+ @container.addClass("#{Mercury.config.regions.className}-preview").removeClass(Mercury.config.regions.className)
174
+ value = @converter.makeHtml(@element.val())
175
+ @previewElement.html(value)
176
+ @previewElement.show()
177
+ @element.hide()
178
+ Mercury.trigger('region:blurred', {region: @})
179
+
180
+
181
+ execCommand: (action, options = {}) ->
182
+ super
183
+
184
+ handler.call(@, @selection(), options) if handler = Mercury.Regions.Markupable.actions[action]
185
+ @resize()
186
+
187
+
188
+ pushHistory: (keyCode) ->
189
+ # when pressing return, delete or backspace it should push to the history
190
+ # all other times it should store if there's a 1 second pause
191
+ keyCodes = [13, 46, 8]
192
+ waitTime = 2.5
193
+ knownKeyCode = keyCodes.indexOf(keyCode) if keyCode
194
+
195
+ # clear any pushes to the history
196
+ clearTimeout(@historyTimeout)
197
+
198
+ # if the key code was return, delete, or backspace store now -- unless it was the same as last time
199
+ if knownKeyCode >= 0 && knownKeyCode != @lastKnownKeyCode # || !keyCode
200
+ @history.push(@contentAndSelection())
201
+ else if keyCode
202
+ # set a timeout for pushing to the history
203
+ @historyTimeout = setTimeout(waitTime * 1000, => @history.push(@contentAndSelection()))
204
+ else
205
+ # push to the history immediately
206
+ @history.push(@contentAndSelection())
207
+
208
+ @lastKnownKeyCode = knownKeyCode
209
+
210
+
211
+ selection: ->
212
+ return new Mercury.Regions.Markupable.Selection(@element)
213
+
214
+
215
+ resize: ->
216
+ @element.css({height: @element.get(0).scrollHeight - 100})
217
+ @element.css({height: @element.get(0).scrollHeight});
218
+
219
+
220
+ snippets: ->
221
+
222
+
223
+ # Actions
224
+ @actions: {
225
+
226
+ undo: -> @content(@history.undo())
227
+
228
+ redo: -> @content(@history.redo())
229
+
230
+ insertHTML: (selection, options) ->
231
+ if options.value.get && element = options.value.get(0)
232
+ options.value = jQuery('<div>').html(element).html()
233
+ selection.replace(options.value, false, true)
234
+
235
+ insertImage: (selection, options) ->
236
+ selection.replace('![add alt text](' + encodeURI(options.value.src) + ')', true)
237
+
238
+ insertTable: (selection, options) ->
239
+ selection.replace(options.value.replace(/<br>|<br\/>/ig, ''), false, true)
240
+
241
+ insertLink: (selection, options) ->
242
+ selection.replace("[#{options.value.content}](#{options.value.attrs.href} 'optional title')", true)
243
+
244
+ insertUnorderedList: (selection) -> selection.addList('unordered')
245
+
246
+ insertOrderedList: (selection) -> selection.addList('ordered')
247
+
248
+ style: (selection, options) -> selection.wrap("<span class=\"#{options.value}\">", '</span>')
249
+
250
+ formatblock: (selection, options) ->
251
+ wrappers = {
252
+ h1: ['# ', ' #']
253
+ h2: ['## ', ' ##']
254
+ h3: ['### ', ' ###']
255
+ h4: ['#### ', ' ####']
256
+ h5: ['##### ', ' #####']
257
+ h6: ['###### ', ' ######']
258
+ pre: [' ', '']
259
+ blockquote: ['> ', '']
260
+ p: ['\n', '\n']
261
+ }
262
+ selection.unWrapLine("#{wrapper[0]}", "#{wrapper[1]}") for wrapperName, wrapper of wrappers
263
+ if options.value == 'blockquote'
264
+ Mercury.Regions.Markupable.actions.indent.call(@, selection, options)
265
+ return
266
+ selection.wrapLine("#{wrappers[options.value][0]}", "#{wrappers[options.value][1]}")
267
+
268
+ bold: (selection) -> selection.wrap('**', '**')
269
+
270
+ italic: (selection) -> selection.wrap('_', '_')
271
+
272
+ subscript: (selection) -> selection.wrap('<sub>', '</sub>')
273
+
274
+ superscript: (selection) -> selection.wrap('<sup>', '</sup>')
275
+
276
+ indent: (selection) ->
277
+ selection.wrapLine('> ', '', false, true)
278
+
279
+ outdent: (selection) ->
280
+ selection.unWrapLine('> ', '', false, true)
281
+
282
+ horizontalRule: (selection) -> selection.replace('\n- - -\n')
283
+
284
+ insertSnippet: (selection, options) ->
285
+ snippet = options.value
286
+ selection.replace(snippet.getText())
287
+
288
+ }
289
+
290
+
291
+ # Helper class for managing selection and getting information from it
292
+ class Mercury.Regions.Markupable.Selection
293
+
294
+ constructor: (@element) ->
295
+ @el = @element.get(0)
296
+ @getDetails()
297
+
298
+
299
+ serialize: ->
300
+ return {start: @start, end: @end}
301
+
302
+
303
+ getDetails: ->
304
+ @length = @el.selectionEnd - @el.selectionStart
305
+ @start = @el.selectionStart
306
+ @end = @el.selectionEnd
307
+ @text = @element.val().substr(@start, @length)
308
+
309
+
310
+ replace: (text, select = false, placeCursor = false) ->
311
+ @getDetails()
312
+ val = @element.val()
313
+ savedVal = @element.val()
314
+ @element.val(val.substr(0, @start) + text + val.substr(@end, val.length))
315
+ changed = @element.val() != savedVal
316
+ @select(@start, @start + text.length) if select
317
+ @select(@start + text.length, @start + text.length) if placeCursor
318
+ return changed
319
+
320
+
321
+ select: (@start, @end) ->
322
+ @element.focus()
323
+ @el.selectionStart = @start
324
+ @el.selectionEnd = @end
325
+ @getDetails()
326
+
327
+
328
+ wrap: (left, right) ->
329
+ @getDetails()
330
+ @deselectNewLines()
331
+ @replace(left + @text + right, @text != '')
332
+ @select(@start + left.length, @start + left.length) if @text == ''
333
+
334
+
335
+ wrapLine: (left, right, selectAfter = true, reselect = false) ->
336
+ @getDetails()
337
+ savedSelection = @serialize()
338
+ text = @element.val()
339
+ start = text.lastIndexOf('\n', @start)
340
+ end = text.indexOf('\n', @end)
341
+ end = text.length if end < start
342
+ start = text.lastIndexOf('\n', @start - 1) if text[start] == '\n'
343
+ @select(start + 1, end)
344
+ @replace(left + @text + right, selectAfter)
345
+ @select(savedSelection.start + left.length, savedSelection.end + left.length) if reselect
346
+
347
+
348
+ unWrapLine: (left, right, selectAfter = true, reselect = false) ->
349
+ @getDetails()
350
+ savedSelection = @serialize()
351
+ text = @element.val()
352
+ start = text.lastIndexOf('\n', @start)
353
+ end = text.indexOf('\n', @end)
354
+ end = text.length if end < start
355
+ start = text.lastIndexOf('\n', @start - 1) if text[start] == '\n'
356
+ @select(start + 1, end)
357
+ window.something = @text
358
+ leftRegExp = new RegExp("^#{left.regExpEscape()}")
359
+ rightRegExp = new RegExp("#{right.regExpEscape()}$")
360
+ changed = @replace(@text.replace(leftRegExp, '').replace(rightRegExp, ''), selectAfter)
361
+ @select(savedSelection.start - left.length, savedSelection.end - left.length) if reselect && changed
362
+
363
+
364
+ addList: (type) ->
365
+ text = @element.val()
366
+ start = text.lastIndexOf('\n', @start)
367
+ end = text.indexOf('\n', @end)
368
+ end = text.length if end < start
369
+ start = text.lastIndexOf('\n', @start - 1) if text[start] == '\n'
370
+ @select(start + 1, end)
371
+ lines = @text.split('\n')
372
+ if type == 'unordered'
373
+ @replace("- " + lines.join("\n- "), true)
374
+ else
375
+ @replace(("#{index + 1}. #{line}" for line, index in lines).join('\n'), true)
376
+
377
+
378
+ deselectNewLines: ->
379
+ text = @text
380
+ length = text.replace(/\n+$/g, '').length
381
+ @select(@start, @start + length)
382
+
383
+
384
+ placeMarker: ->
385
+ @wrap('[mercury-marker]', '[mercury-marker]')
386
+
387
+
388
+ removeMarker: ->
389
+ val = @element.val()
390
+ start = val.indexOf('[mercury-marker]')
391
+ return unless start > -1
392
+ end = val.indexOf('[mercury-marker]', start + 1) - '[mercury-marker]'.length
393
+ @element.val(@element.val().replace(/\[mercury-marker\]/g, ''))
394
+ @select(start, end)
395
+
396
+
397
+ textContent: ->
398
+ return @text