1 define(function(require) {
6 var _ = require('libs/underscore'),
7 templates = require('plugins/core/templates'),
8 footnote = require('plugins/core/footnote'),
9 switchTo = require('plugins/core/switch'),
10 lists = require('plugins/core/lists'),
11 plugin = {name: 'core', actions: [], canvas: {}, documentExtension: {textNode: {}}},
12 Dialog = require('views/dialog/dialog');
15 plugin.documentExtension.textNode.transformations = {
17 impl: function(args) {
20 newNodes = node.split({offset: args.offset});
21 [newNodes.first, newNodes.second].some(function(newNode) {
22 if(!(newNode.contents().length)) {
23 emptyText = newNode.append({text: ''});
27 return _.extend(newNodes, {emptyText: emptyText});
29 getChangeRoot: function() {
30 return this.context.parent().parent();
33 mergeContentUp: function() {
39 if(myPrev.nodeType === Node.TEXT_NODE) {
40 if(myPrev.getIndex() > 0) {
43 myPrev = base = myPrev.parent();
46 myPrev = myPrev && myPrev.prev();
48 if(myPrev && myPrev.nodeType === Node.ELEMENT_NODE) {
54 ret = myPrev.append(ptr);
64 return {node: ret, offset: ret.sameNode(this) ? null : ret.getText().length - this.getText().length};
69 var undoRedoAction = function(dir) {
73 document: {type: 'context', name: 'document'},
76 label: dir === 'undo' ? '<-' : '->',
78 iconStyle: dir === 'undo' ? '-webkit-transform: scale(-1,1); transform: scale(-1, 1)' : '',
79 execute: function(callback, params) {
80 params.document[dir]();
84 getState: function(params) {
85 var allowed = params.document && !!(params.document[dir+'Stack'].length),
86 desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'),
87 descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo');
89 var metadata = _.last(params.document[dir+'Stack']).metadata;
91 desc += ': ' + (metadata.description || gettext('unknown operation'));
96 description: allowed ? desc : descEmpty
102 var pad = function(number) {
104 number = '0' + number;
109 var commentAction = {
112 fragment: {type: 'context', name: 'fragment'}
116 execute: function(callback, params, editor) {
118 var node = params.fragment.node,
120 if(node.nodeType === Node.TEXT_NODE) {
121 node = node.parent();
123 node.document.transaction(function() {
124 var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}});
125 comment.append({text:''});
126 var user = editor.getUser(), creator;
130 creator += ' (' + user.email + ')';
133 creator = 'anonymous';
136 var currentDate = new Date(),
137 dt = pad(currentDate.getDate()) + '-' +
138 pad((currentDate.getMonth() + 1)) + '-' +
139 pad(currentDate.getFullYear()) + ' ' +
140 pad(currentDate.getHours()) + ':' +
141 pad(currentDate.getMinutes()) + ':' +
142 pad(currentDate.getSeconds());
144 var metadata = comment.getMetadata();
145 metadata.add({key: 'creator', value: creator});
146 metadata.add({key: 'date', value: dt});
149 description: action.getState().description
155 getState: function(params) {
157 allowed: params.fragment && params.fragment.isValid() &&
158 params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
161 state.description = gettext('Insert comment after current node');
168 var createWrapTextAction = function(createParams) {
170 name: createParams.name,
172 fragment: {type: 'context', name: 'fragment'},
174 getState: function(params) {
176 label: this.config.label
181 !params.fragment || !params.fragment.isValid() ||
182 !(params.fragment instanceof params.fragment.TextRangeFragment) ||
183 !params.fragment.hasSiblingBoundries()) {
184 return _.extend(state, {allowed: false});
187 parent = params.fragment.startNode.parent();
188 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
189 return _.extend(state, {allowed: false});
192 return _.extend(state, {
194 description: createParams.description,
195 execute: function(callback, params) {
196 params.fragment.document.transaction(function() {
197 var parent = params.fragment.startNode.parent(),
198 doc = params.fragment.document,
199 wrapper, lastTextNode;
201 wrapper = parent.wrapText({
202 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
203 offsetStart: params.fragment.startOffset,
204 offsetEnd: params.fragment.endOffset,
205 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
208 lastTextNode = wrapper.getLastTextNode();
210 return doc.createFragment(doc.CaretFragment, {node: lastTextNode, offset: lastTextNode.getText().length});
214 description: createParams.description
225 var createLinkFromSelection = function(callback, params) {
226 var doc = params.fragment.document,
227 dialog = Dialog.create({
228 title: gettext('Create link'),
229 executeButtonText: gettext('Apply'),
230 cancelButtonText: gettext('Cancel'),
232 {label: gettext('Link'), name: 'href', type: 'input'}
237 dialog.on('execute', function(event) {
238 doc.transaction(function() {
239 var span = action.params.fragment.startNode.parent().wrapText({
240 _with: {tagName: 'span', attrs: {'class': 'link'}},
241 offsetStart: params.fragment.startOffset,
242 offsetEnd: params.fragment.endOffset,
243 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
245 doc = params.fragment.document;
246 span.setAttr('href', event.formData.href);
248 return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
251 description: action.getState().description
259 var editLink = function(callback, params) {
260 var doc = params.fragment.document,
261 link = params.fragment.node.getParent('link'),
262 dialog = Dialog.create({
263 title: gettext('Edit link'),
264 executeButtonText: gettext('Apply'),
265 cancelButtonText: gettext('Cancel'),
267 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
272 dialog.on('execute', function(event) {
273 doc.transaction(function() {
274 link.setAttr('href', event.formData.href);
276 return params.fragment;
279 description: action.getState().description
290 fragment: {type: 'context', name: 'fragment'}
293 label: gettext('link')
295 getState: function(params) {
296 if(!params.fragment || !params.fragment.isValid()) {
297 return {allowed: false};
300 if(params.fragment instanceof params.fragment.TextRangeFragment) {
301 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
302 return {allowed: false};
306 description: gettext('Create link from selection'),
307 execute: createLinkFromSelection
311 if(params.fragment instanceof params.fragment.CaretFragment) {
312 if(params.fragment.node.isInside('link')) {
313 return {allowed: true, toggled: true, execute: editLink};
316 return {allowed: false};
322 undoRedoAction('undo'),
323 undoRedoAction('redo'),
325 createWrapTextAction({name: 'emphasis', klass: 'emp', description: gettext('Mark as emphasized')}),
326 createWrapTextAction({name: 'cite', klass: 'cite', description: gettext('Mark as citation')}),
328 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
332 plugin.config = function(config) {
333 // templates.actions[0].config(config.templates);
334 templates.actions[0].params.template.options = config.templates;