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) {
91 from.contents().forEach(function(node) {
100 return toMerge.is('p');
103 if(prev && prev.is('p') || prev.is({tagName: 'header'})) {
104 merge(toMerge, prev);
106 if(prev && prev.is('list')) {
107 var items = prev.contents().filter(function(n) { return n.is('item');});
108 merge(toMerge, items[items.length-1]);
109 //return {node: toMerge, offset:0};
114 applies: function() {
115 return toMerge.is({tagName: 'header'});
118 if(prev && prev.is('p') || prev.is({tagName: 'header'})) {
119 merge(toMerge, prev);
124 applies: function() {
125 return toMerge.is('item');
129 if(prev && prev.is('item')) {
130 merge(toMerge, prev);
131 } else if(!prev && (list = toMerge.parent()) && list.is('list')) {
132 list.before(toMerge);
133 toMerge.setClass('p');
134 if(!list.contents().length) {
137 return {node: toMerge.contents()[0], offset:0};
144 strategies.some(function(strategy) {
145 if(strategy.applies()) {
146 toret = strategy.run();
154 var undoRedoAction = function(dir) {
158 document: {type: 'context', name: 'document'},
161 label: dir === 'undo' ? '<-' : '->',
163 iconStyle: dir === 'undo' ? '-webkit-transform: scale(-1,1); transform: scale(-1, 1)' : '',
164 execute: function(callback, params) {
165 var metadata = _.last(params.document[dir+'Stack']).metadata,
166 fragment = metadata && metadata.fragment;
167 params.document[dir]();
169 if(!fragment.isValid()) {
170 fragment.restoreFromPaths();
172 if(fragment.isValid()) {
179 getState: function(params) {
180 var allowed = params.document && !!(params.document[dir+'Stack'].length),
181 desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'),
182 descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo');
184 var metadata = _.last(params.document[dir+'Stack']).metadata;
186 desc += ': ' + (metadata.description || gettext('unknown operation'));
191 description: allowed ? desc : descEmpty
197 var pad = function(number) {
199 number = '0' + number;
204 var commentAction = {
207 fragment: {type: 'context', name: 'fragment'}
211 execute: function(callback, params, editor) {
213 var node = params.fragment.node,
215 if(node.nodeType === Node.TEXT_NODE) {
216 node = node.parent();
218 node.document.transaction(function() {
219 var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}});
220 comment.append({text:''});
221 var user = editor.getUser(), creator;
225 creator += ' (' + user.email + ')';
228 creator = 'anonymous';
231 var currentDate = new Date(),
232 dt = pad(currentDate.getDate()) + '-' +
233 pad((currentDate.getMonth() + 1)) + '-' +
234 pad(currentDate.getFullYear()) + ' ' +
235 pad(currentDate.getHours()) + ':' +
236 pad(currentDate.getMinutes()) + ':' +
237 pad(currentDate.getSeconds());
239 var metadata = comment.getMetadata();
240 metadata.add({key: 'creator', value: creator});
241 metadata.add({key: 'date', value: dt});
244 description: action.getState().description
250 getState: function(params) {
252 allowed: params.fragment && params.fragment.isValid() &&
253 params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
256 state.description = gettext('Insert comment');
263 var createWrapTextAction = function(createParams) {
265 name: createParams.name,
267 fragment: {type: 'context', name: 'fragment'},
269 getState: function(params) {
271 label: this.config.label
275 if(!params.fragment || !params.fragment.isValid()) {
276 return _.extend(state, {allowed: false});
279 if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside(createParams.klass)) {
280 return _.extend(state, {
283 description: createParams.unwrapDescription,
284 execute: function(callback, params) {
285 var node = params.fragment.node,
287 toRemove = node.getParent(createParams.klass),
290 if(node.sameNode(toRemove.contents()[0]) && toRemove.isPrecededByTextNode()) {
291 prefLen = toRemove.prev().getText().length;
294 doc.transaction(function() {
295 var ret = toRemove.unwrapContent(),
296 newFragment = params.fragment;
297 if(!newFragment.isValid()) {
298 newFragment = doc.createFragment(doc.CaretFragment, {
300 offset: prefLen + params.fragment.offset
306 description: createParams.unwrapDescription,
307 fragment: params.fragment
315 if(params.fragment instanceof params.fragment.TextRangeFragment && params.fragment.hasSiblingBoundries()) {
316 parent = params.fragment.startNode.parent();
317 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
318 return _.extend(state, {allowed: false});
321 return _.extend(state, {
323 description: createParams.wrapDescription,
324 execute: function(callback, params) {
325 params.fragment.document.transaction(function() {
326 var parent = params.fragment.startNode.parent(),
327 doc = params.fragment.document,
328 wrapper, lastTextNode;
330 wrapper = parent.wrapText({
331 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
332 offsetStart: params.fragment.startOffset,
333 offsetEnd: params.fragment.endOffset,
334 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
337 lastTextNode = wrapper.getLastTextNode();
339 return doc.createFragment(doc.CaretFragment, {node: lastTextNode, offset: lastTextNode.getText().length});
343 description: createParams.wrapDescription,
344 fragment: params.fragment
351 return _.extend(state, {allowed: false});
357 var createLinkFromSelection = function(callback, params) {
358 var doc = params.fragment.document,
359 dialog = Dialog.create({
360 title: gettext('Create link'),
361 executeButtonText: gettext('Apply'),
362 cancelButtonText: gettext('Cancel'),
364 {label: gettext('Link'), name: 'href', type: 'input',
365 prePasteHandler: function(text) {
366 return params.fragment.document.getLinkForUrl(text);
373 dialog.on('execute', function(event) {
374 doc.transaction(function() {
375 var span = action.params.fragment.startNode.parent().wrapText({
376 _with: {tagName: 'span', attrs: {'class': 'link', href: event.formData.href }},
377 offsetStart: params.fragment.startOffset,
378 offsetEnd: params.fragment.endOffset,
379 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
381 doc = params.fragment.document;
383 return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
386 description: action.getState().description,
387 fragment: params.fragment
395 var editLink = function(callback, params) {
396 var doc = params.fragment.document,
397 link = params.fragment.node.getParent('link'),
398 dialog = Dialog.create({
399 title: gettext('Edit link'),
400 executeButtonText: gettext('Apply'),
401 cancelButtonText: gettext('Cancel'),
403 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
408 dialog.on('execute', function(event) {
409 doc.transaction(function() {
410 link.setAttr('href', event.formData.href);
412 return params.fragment;
415 description: action.getState().description,
416 fragment: params.fragment
427 fragment: {type: 'context', name: 'fragment'}
430 label: gettext('link')
432 getState: function(params) {
433 if(!params.fragment || !params.fragment.isValid()) {
434 return {allowed: false};
437 if(params.fragment instanceof params.fragment.TextRangeFragment) {
438 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
439 return {allowed: false};
443 description: gettext('Create link from selection'),
444 execute: createLinkFromSelection
448 if(params.fragment instanceof params.fragment.CaretFragment) {
449 if(params.fragment.node.isInside('link')) {
453 description: gettext('Edit link'),
458 return {allowed: false};
462 var metadataParams = {};
465 undoRedoAction('undo'),
466 undoRedoAction('redo'),
468 createWrapTextAction({name: 'emphasis', klass: 'emp', wrapDescription: gettext('Mark as emphasized'), unwrapDescription: gettext('Remove emphasis')}),
469 createWrapTextAction({name: 'cite', klass: 'cite', wrapDescription: gettext('Mark as citation'), unwrapDescription: gettext('Remove citation')}),
471 metadataEditor.action(metadataParams)
472 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
476 plugin.config = function(config) {
477 // templates.actions[0].config(config.templates);
478 templates.actions[0].params.template.options = config.templates;
479 metadataParams.config = (config.metadata || []).sort(function(configRow1, configRow2) {
480 if(configRow1.key < configRow2.key) {
483 if(configRow1.key > configRow2.key) {
490 plugin.canvasElements = canvasElements;