This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
text.js
139 lines (102 loc) · 3.01 KB
/
text.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
(function (w) {
// Substance
// =========
if (!w.Substance || !Substance) { w.Substance = Substance = {}; }
// Suggested interactions with surface
// ====================================
// Update the UI
// -------------
// $('a.toggle-annotation').removeClass('active')
// annotations.each(function(annotation) {
// $('a.toggle-annotation.'+annotation.type).addClass('active');
// });
// });
// Hooking into selection events is easy too.
// el is a container html element sitting below the selection.
// You can populate it with some contextual UI stuff.
//
// surface.on('selection', function(selection, el) {
// $(el).html('<a href="#" class="em">Emphasize</a>');
// });
// Text
// =====
Substance.Text = function(options) {
// Helpers
// -------------
_.tpl = function(tpl, ctx) {
source = $("script[name="+tpl+"]").html();
return _.template(source, ctx);
};
// small function to poppulate tool links
function addTools(_type, callback){
var text
, type;
if(typeof _type === 'object'){
if(typeof _type.type === 'undefined'){
_type.type = 'cmd';
}
text = _type.text || _type.type
type = _type.type;
}else{
text = type = _type;
}
$tool = $(_.tpl('tool', {'type':type, 'text':text}));
$tool.click(type, callback);
$toolbar.append($tool);
}
var $el = $(options.el)
, styles = ['em', 'str']
, $toolbar = $($('script[name=toolbar]').html())
, surface = new Substance.Surface({
el: options.el,
content: options.content,
annotations: []
});
$el.wrap('<div class="text" />');
$text = $('.text');
$toolbar.insertBefore($el);
// Regular text attributes
$.each(styles, function(k, type){
addTools(type, function(){
surface.apply(["insert", {"type": type}]);
});
});
// Adding links
addTools({type:'link', text:'Link'}, function(type){
// optional link attributes
var attr = {
"href" : "#",
"prompt" : "Add a valid URL",
"placeholder" : "http://"
};
surface.apply(["insert", {"type": type, "attributes": attr}]);
});
// Adding comments
addTools({type:'comment', text:'Add comment'}, function(type){
surface.apply(["insert", {"type": type}]);
});
// Adding comments
addTools({text:'Test programmatic selection (before and after annotations)'}, function(type){
surface.select(305, 310);
});
// Events
// ------
surface.on('surface:active', function(){
$text.addClass('active');
});
surface.on('surface:inactive', function(){
$text.removeClass('active');
});
// Returns all annotations matching that selection
surface.on('selection:change', function(sel) {
// var annotation = surface.annotations(sel);
console.log('selection:change', sel);
});
// Delegate Events
// ------
this.on = surface.on;
}
// Substance.Text.prototype.on = function() {
// this.surface.on.apply(this, arguments);
// }
})(window);