This repository has been archived by the owner on May 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsandbox.coffee
123 lines (110 loc) · 4.26 KB
/
sandbox.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
Aloha.ready ->
###
Generate a little button next to the cursor that allows quick access to adding semantic tags around text or converting bland elements like sections (headings) into more semantically rich elements like exercises
###
###
Also, has code for supporting drag-and-drop handles
###
# Helper functions to get the exact position of the editable cursor (caret)
getSelectionRect = (rangeObject) ->
unless rangeObject.getStartDocumentPos
console.error "Couldn't call getStartDocumentPos on rangy (wrong ver of rangy and proper plugins not installed"
return null
start: rangeObject.getStartDocumentPos()
end: rangeObject.getEndDocumentPos()
showContext = (rangeObject) ->
selection = getSelectionRect(rangeObject)
if selection
position = {}
position.left = selection.end.x
position.top = selection.end.y
$context.css position
$icon.fadeIn "fast"
else
$icon.hide()
# Create the elements needed for context popup
$document = Aloha.jQuery(".document")
$overlay = Aloha.jQuery("body")
$context = Aloha.jQuery("<div class=\"context-cursor\"/>").appendTo($overlay)
$icon = Aloha.jQuery("<div class=\"context-icon\"> ^</div>").hide().appendTo($context)
$menu = Aloha.jQuery("<div class=\"context-menu\"/>").hide().appendTo($context)
# These are the semanticly rich buttons
buttons = []
buttons.push
title: "Term"
command: "cmd-term"
markup: "<span class=\"term\"/>"
shortcut: "ctrl+shift+t"
buttons.push
title: "Note"
command: "cmd-note"
markup: "<span class=\"note\"/>"
shortcut: "ctrl+shift+n"
buttons.push
title: "Quote"
command: "cmd-quote"
markup: "<q />"
shortcut: "ctrl+shift+q"
buttons.push
title: "Footnote"
command: "cmd-footnote"
markup: "<span class=\"footnote\"/>"
shortcut: "ctrl+shift+f"
for cmd in buttons
$cmd = Aloha.jQuery("<div href=\"#\" title=\"" + cmd.title + " (" + cmd.shortcut + ")\" class=\"command " + cmd.command + "\" command=\"" + cmd.command + "\"><span>" + cmd.title + "</span></div>")
$cmd.click ->
markup = jQuery(cmd.markup)
rangeObject = Aloha.Selection.rangeObject
GENTICS.Utils.Dom.extendToWord rangeObject if rangeObject.isCollapsed()
Aloha.Selection.changeMarkupOnSelection Aloha.jQuery(markup)
$cmd.appendTo $menu
$icon.bind "click", (evt) ->
evt.stopPropagation() # Stop propagation so the editor doesn't lose focus
$menu.show()
$overlay.click ->
$menu.hide()
$icon.hide()
$handle = Aloha.jQuery("<div class=\"handle\" contenteditable=\"false\"></div>").hide().appendTo("body")
changeHandler = (event, rangeObject) ->
sel = rangy.getSelection()
ranges = sel.getAllRanges()
return if ranges.length is 0
range = rangeObject || ranges[0]
start = range.startContainer
end = range.endContainer
if rangeObject.getStartDocumentPos
showContext rangeObject
else
console.warn "Not displaying context icon because rangy is missing the rangy-position plugin for pixel-calculation of cursor position"
$icon.hide()
$start = Aloha.jQuery(start).parent("h1,h2,h3,h4,h5,h6,p")
$end = Aloha.jQuery(end).parent("h1,h2,h3,h4,h5,h6,p")
css = {}
if $start.offset() != null and $end.offset() != null
css.top = $start.offset().top
css.height = $end.offset().top + $end.height() - css.top
$handle.data node: $start
$handle.css(css).show()
else
$handle.hide()
Aloha.bind "aloha-selection-changed", changeHandler
# On focus the cursor isn't available yet so fire the event after a period of time
$document.bind "focus", (evt) ->
setTimeout (() -> changeHandler evt, Aloha.Selection.rangeObject), 10
dragScope = "blockish-nodes-only"
$handle.draggable
scope: dragScope
revert: "invalid"
cursor: "move"
start: (el) ->
$blocks = $document.find("h1,h2,h3,h4,h5,h6,p")
$blocks.droppable
scope: dragScope
hoverClass: "drop-before"
drop: (event, ui) ->
ui.draggable.data("node").insertBefore Aloha.jQuery(this)
ui.draggable.attr "style", ""
Aloha.ready ->
Aloha.jQuery("<script src=\"./lib/jquery-ui.min.js\"></script>").appendTo "body"
$document = Aloha.jQuery(".document")
$document.aloha()