mirrored from git://git.moodle.org/moodle.git
/
moodle-atto_indent-button-debug.js
227 lines (196 loc) · 8.1 KB
/
moodle-atto_indent-button-debug.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
YUI.add('moodle-atto_indent-button', function (Y, NAME) {
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/*
* @package atto_indent
* @copyright 2013 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* @module moodle-atto_indent-button
*/
/**
* Atto text editor indent plugin.
*
* @namespace M.atto_indent
* @class button
* @extends M.editor_atto.EditorPlugin
*/
Y.namespace('M.atto_indent').Button = Y.Base.create('button', Y.M.editor_atto.EditorPlugin, [], {
initializer: function() {
this.addButton({
icon: 'e/decrease_indent',
title: 'outdent',
buttonName: 'outdent',
callback: this.outdent
});
this.addButton({
icon: 'e/increase_indent',
title: 'indent',
buttonName: 'indent',
callback: this.indent
});
},
/**
* Indents the currently selected content.
*
* @method indent
*/
indent: function() {
// Save the current selection - we want to restore this.
var selection = window.rangy.saveSelection(),
blockquotes = this.editor.all('blockquote'),
count = blockquotes.size();
// Remove display:none from rangy markers so browser doesn't delete them.
this.editor.all('.rangySelectionBoundary').setStyle('display', null);
// Mark all existing block quotes in case the user has actually added some.
blockquotes.addClass('pre-existing');
// Run the indent command.
document.execCommand('indent', false, null);
// Fix indent list item.
this.fixupListItemsAfterIndent();
// Get all blockquotes, both existing and new.
blockquotes = this.editor.all('blockquote');
if (blockquotes.size() !== count) {
// There are new block quotes, the indent exec has wrapped some content in block quotes in order
// to indent the selected content.
// We don't want blockquotes, we're going to convert them to divs.
this.replaceBlockquote(this.editor);
// Finally restore the seelction. The content has changed - sometimes this works - but not always :(
window.rangy.restoreSelection(selection);
} else if (blockquotes.size() > 0) {
// There were no new blockquotes, this happens if the user is indenting/outdenting a list.
blockquotes.removeClass('pre-existing');
}
// Remove the selection markers - a clean up really.
window.rangy.removeMarkers(selection);
// Mark the text as having been updated.
this.markUpdated();
},
/**
* Outdents the currently selected content.
*
* @method outdent
*/
outdent: function() {
// Save the selection we will want to restore it.
var selection = window.rangy.saveSelection(),
blockquotes = this.editor.all('blockquote'),
count = blockquotes.size();
// Mark existing blockquotes so that we don't convert them later.
blockquotes.addClass('pre-existing');
// Replace all div indents with blockquote indents so that we can rely on the browser functionality.
this.replaceEditorIndents(this.editor);
// Restore the users selection - otherwise the next outdent operation won't work!
window.rangy.restoreSelection(selection);
// And save it once more.
selection = window.rangy.saveSelection();
// Outdent.
document.execCommand('outdent', false, null);
// Get all blockquotes so that we can work out what happened.
blockquotes = this.editor.all('blockquote');
if (blockquotes.size() !== count) {
// The number of blockquotes hasn't changed.
// This occurs when the user has outdented a list item.
this.replaceBlockquote(this.editor);
window.rangy.restoreSelection(selection);
} else if (blockquotes.size() > 0) {
// The number of blockquotes is the same and is more than 0 we just need to clean up the class
// we added to mark pre-existing blockquotes.
blockquotes.removeClass('pre-existing');
}
// Clean up any left over selection markers.
window.rangy.removeMarkers(selection);
// Mark the text as having been updated.
this.markUpdated();
},
/**
* Replaces all blockquotes within an editor with div indents.
* @method replaceBlockquote
* @param Editor editor
*/
replaceBlockquote: function(editor) {
editor.all('blockquote').setAttribute('data-iterate', true);
var blockquote = editor.one('blockquote'),
margindir = (Y.one('body.dir-ltr')) ? 'marginLeft' : 'marginRight';
while (blockquote) {
blockquote.removeAttribute('data-iterate');
if (blockquote.hasClass('pre-existing')) {
blockquote.removeClass('pre-existing');
} else {
var clone = Y.Node.create('<div></div>')
.setAttrs(blockquote.getAttrs())
.setStyle(margindir, '30px')
.addClass('editor-indent');
// We use childNodes here because we are interested in both type 1 and 3 child nodes.
var children = blockquote.getDOMNode().childNodes;
var child;
child = children[0];
while (typeof child !== "undefined") {
clone.append(child);
child = children[0];
}
blockquote.replace(clone);
}
blockquote = editor.one('blockquote[data-iterate]');
}
},
/**
* Replaces all div indents with blockquotes.
* @method replaceEditorIndents
* @param Editor editor
*/
replaceEditorIndents: function(editor) {
// We use the editor-indent class because it is preserved between saves.
var indent = editor.one('.editor-indent');
while (indent) {
var clone = Y.Node.create('<blockquote></blockquote>')
.setAttrs(indent
.getAttrs())
.removeClass('editor-indent');
// We use childNodes here because we are interested in both type 1 and 3 child nodes.
var children = indent.getDOMNode().childNodes;
var child;
child = children[0];
while (typeof child !== "undefined") {
clone.append(child);
child = children[0];
}
indent.replace(clone);
indent = editor.one('.editor-indent');
}
},
/**
* Fixup for list item after indent.
*
* @method fixupListItemsAfterIndent
*/
fixupListItemsAfterIndent: function() {
var selection = window.rangy.getSelection(),
rootelement = this.editor.getDOMNode(),
listelement = selection.anchorNode.parentElement;
listelement = listelement.closest('ol, ul');
if (!(listelement && rootelement.contains(listelement))) {
return;
}
// We will move the child list into previous list item of the parent.
var previous = listelement.previousElementSibling;
if (previous && previous.tagName === 'LI') {
previous.appendChild(listelement);
selection.collapseToEnd();
}
}
});
}, '@VERSION@', {"requires": ["moodle-editor_atto-plugin"]});