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: {}, elementNode: {}}},
12 Dialog = require('views/dialog/dialog'),
13 canvasElements = require('plugins/core/canvasElements'),
14 metadataEditor = require('plugins/core/metadataEditor/metadataEditor');
18 plugin.documentExtension.textNode.transformations = {
20 impl: function(args) {
22 parentDescribingNodes = [],
24 newNodes = node.split({offset: args.offset});
25 newNodes.second.contents()
26 .filter(function(child) {
27 return child.object.describesParent;
29 .forEach(function(child) {
31 parentDescribingNodes.push(child);
34 [newNodes.first, newNodes.second].some(function(newNode) {
35 if(!(newNode.contents().length)) {
36 emptyText = newNode.append({text: ''});
40 parentDescribingNodes.forEach(function(node) {
41 newNodes.first.append(node);
43 return _.extend(newNodes, {emptyText: emptyText});
45 getChangeRoot: function() {
46 return this.context.parent().parent();
49 mergeContentUp: function() {
55 if(myPrev.nodeType === Node.TEXT_NODE) {
56 if(myPrev.getIndex() > 0) {
59 myPrev = base = myPrev.parent();
62 myPrev = myPrev && myPrev.prev();
64 if(myPrev && myPrev.nodeType === Node.ELEMENT_NODE) {
70 ret = myPrev.append(ptr);
80 return {node: ret, offset: ret.sameNode(this) ? null : ret.getText().length - this.getText().length};
85 plugin.documentExtension.elementNode.transformations = {
88 prev = toMerge.prev();
90 var merge = function(from, to) {
92 from.contents().forEach(function(node, idx) {
94 if(idx === 0 && node.nodeType === Node.TEXT_NODE) {
95 len = node.getText().length;
97 ret = to.append(node);
102 offset: ret.getText().length - len
112 applies: function() {
113 return toMerge.is('p');
116 if(prev && prev.is('p') || prev.is({tagName: 'header'})) {
117 return merge(toMerge, prev);
119 if(prev && prev.is('list')) {
120 var items = prev.contents().filter(function(n) { return n.is('item');});
121 return merge(toMerge, items[items.length-1]);
126 applies: function() {
127 return toMerge.is({tagName: 'header'});
130 if(prev && prev.is('p') || prev.is({tagName: 'header'})) {
131 return merge(toMerge, prev);
136 applies: function() {
137 return toMerge.is('item');
141 if(prev && prev.is('item')) {
142 return merge(toMerge, prev);
143 } else if(!prev && (list = toMerge.parent()) && list.is('list')) {
144 list.before(toMerge);
145 toMerge.setClass('p');
146 if(!list.contents().length) {
149 return {node: toMerge.contents()[0], offset:0};
156 strategies.some(function(strategy) {
157 if(strategy.applies()) {
158 toret = strategy.run();
166 var undoRedoAction = function(dir) {
170 document: {type: 'context', name: 'document'},
173 label: dir === 'undo' ? '<-' : '->',
175 iconStyle: dir === 'undo' ? '-webkit-transform: scale(-1,1); transform: scale(-1, 1)' : '',
176 execute: function(callback, params) {
177 var metadata = _.last(params.document[dir+'Stack']).metadata,
178 fragment = metadata && metadata.fragment;
179 params.document[dir]();
181 if(!fragment.isValid()) {
182 fragment.restoreFromPaths();
184 if(fragment.isValid()) {
191 getState: function(params) {
192 var allowed = params.document && !!(params.document[dir+'Stack'].length),
193 desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'),
194 descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo');
196 var metadata = _.last(params.document[dir+'Stack']).metadata;
198 desc += ': ' + (metadata.description || gettext('unknown operation'));
203 description: allowed ? desc : descEmpty
209 var pad = function(number) {
211 number = '0' + number;
216 var commentAction = {
219 fragment: {type: 'context', name: 'fragment'}
223 execute: function(callback, params, editor) {
225 var node = params.fragment.node,
227 if(node.nodeType === Node.TEXT_NODE) {
228 node = node.parent();
230 node.document.transaction(function() {
231 var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}});
232 comment.append({text:''});
233 var user = editor.getUser(), creator;
237 creator += ' (' + user.email + ')';
240 creator = 'anonymous';
243 var currentDate = new Date(),
244 dt = pad(currentDate.getDate()) + '-' +
245 pad((currentDate.getMonth() + 1)) + '-' +
246 pad(currentDate.getFullYear()) + ' ' +
247 pad(currentDate.getHours()) + ':' +
248 pad(currentDate.getMinutes()) + ':' +
249 pad(currentDate.getSeconds());
251 var metadata = comment.getMetadata();
252 metadata.add({key: 'creator', value: creator});
253 metadata.add({key: 'date', value: dt});
256 description: action.getState().description
262 getState: function(params) {
264 allowed: params.fragment && params.fragment.isValid() &&
265 params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
268 state.description = gettext('Insert comment');
275 var createWrapTextAction = function(createParams) {
277 name: createParams.name,
279 fragment: {type: 'context', name: 'fragment'},
281 getState: function(params) {
283 label: this.config.label
287 if(!params.fragment || !params.fragment.isValid()) {
288 return _.extend(state, {allowed: false});
291 if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside(createParams.klass)) {
292 return _.extend(state, {
295 description: createParams.unwrapDescription,
296 execute: function(callback, params) {
297 var node = params.fragment.node,
299 toRemove = node.getParent(createParams.klass),
302 if(node.sameNode(toRemove.contents()[0]) && toRemove.isPrecededByTextNode()) {
303 prefLen = toRemove.prev().getText().length;
306 doc.transaction(function() {
307 var ret = toRemove.unwrapContent(),
308 newFragment = params.fragment;
309 if(!newFragment.isValid()) {
310 newFragment = doc.createFragment(doc.CaretFragment, {
312 offset: prefLen + params.fragment.offset
318 description: createParams.unwrapDescription,
319 fragment: params.fragment
327 if(params.fragment instanceof params.fragment.TextRangeFragment && params.fragment.hasSiblingBoundries()) {
328 parent = params.fragment.startNode.parent();
329 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
330 return _.extend(state, {allowed: false});
333 return _.extend(state, {
335 description: createParams.wrapDescription,
336 execute: function(callback, params) {
337 params.fragment.document.transaction(function() {
338 var parent = params.fragment.startNode.parent(),
339 doc = params.fragment.document,
340 wrapper, lastTextNode;
342 wrapper = parent.wrapText({
343 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
344 offsetStart: params.fragment.startOffset,
345 offsetEnd: params.fragment.endOffset,
346 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
349 lastTextNode = wrapper.getLastTextNode();
351 return doc.createFragment(doc.CaretFragment, {node: lastTextNode, offset: lastTextNode.getText().length});
355 description: createParams.wrapDescription,
356 fragment: params.fragment
363 return _.extend(state, {allowed: false});
369 var createLinkFromSelection = function(callback, params) {
370 var doc = params.fragment.document,
371 dialog = Dialog.create({
372 title: gettext('Create link'),
373 executeButtonText: gettext('Apply'),
374 cancelButtonText: gettext('Cancel'),
376 {label: gettext('Link'), name: 'href', type: 'input',
377 prePasteHandler: function(text) {
378 return params.fragment.document.getLinkForUrl(text);
385 dialog.on('execute', function(event) {
386 doc.transaction(function() {
387 var span = action.params.fragment.startNode.parent().wrapText({
388 _with: {tagName: 'span', attrs: {'class': 'link', href: event.formData.href }},
389 offsetStart: params.fragment.startOffset,
390 offsetEnd: params.fragment.endOffset,
391 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
393 doc = params.fragment.document;
395 return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
398 description: action.getState().description,
399 fragment: params.fragment
407 var editLink = function(callback, params) {
408 var doc = params.fragment.document,
409 link = params.fragment.node.getParent('link'),
410 dialog = Dialog.create({
411 title: gettext('Edit link'),
412 executeButtonText: gettext('Apply'),
413 cancelButtonText: gettext('Cancel'),
415 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
420 dialog.on('execute', function(event) {
421 doc.transaction(function() {
422 link.setAttr('href', event.formData.href);
424 return params.fragment;
427 description: action.getState().description,
428 fragment: params.fragment
439 fragment: {type: 'context', name: 'fragment'}
442 label: gettext('link')
444 getState: function(params) {
445 if(!params.fragment || !params.fragment.isValid()) {
446 return {allowed: false};
449 if(params.fragment instanceof params.fragment.TextRangeFragment) {
450 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
451 return {allowed: false};
455 description: gettext('Create link from selection'),
456 execute: createLinkFromSelection
460 if(params.fragment instanceof params.fragment.CaretFragment) {
461 if(params.fragment.node.isInside('link')) {
465 description: gettext('Edit link'),
470 return {allowed: false};
474 var metadataParams = {};
477 undoRedoAction('undo'),
478 undoRedoAction('redo'),
480 createWrapTextAction({name: 'emphasis', klass: 'emp', wrapDescription: gettext('Mark as emphasized'), unwrapDescription: gettext('Remove emphasis')}),
481 createWrapTextAction({name: 'cite', klass: 'cite', wrapDescription: gettext('Mark as citation'), unwrapDescription: gettext('Remove citation')}),
483 metadataEditor.action(metadataParams)
484 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
488 plugin.config = function(config) {
489 // templates.actions[0].config(config.templates);
490 templates.actions[0].params.template.options = config.templates;
491 metadataParams.config = (config.metadata || []).sort(function(configRow1, configRow2) {
492 if(configRow1.key < configRow2.key) {
495 if(configRow1.key > configRow2.key) {
502 plugin.canvasElements = canvasElements;