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'),
13 canvasElements = require('plugins/core/canvasElements');
17 plugin.documentExtension.textNode.transformations = {
19 impl: function(args) {
21 parentDescribingNodes = [],
23 newNodes = node.split({offset: args.offset});
24 newNodes.second.contents()
25 .filter(function(child) {
26 return child.object.describesParent;
28 .forEach(function(child) {
30 parentDescribingNodes.push(child);
33 [newNodes.first, newNodes.second].some(function(newNode) {
34 if(!(newNode.contents().length)) {
35 emptyText = newNode.append({text: ''});
39 parentDescribingNodes.forEach(function(node) {
40 newNodes.first.append(node);
42 return _.extend(newNodes, {emptyText: emptyText});
44 getChangeRoot: function() {
45 return this.context.parent().parent();
48 mergeContentUp: function() {
54 if(myPrev.nodeType === Node.TEXT_NODE) {
55 if(myPrev.getIndex() > 0) {
58 myPrev = base = myPrev.parent();
61 myPrev = myPrev && myPrev.prev();
63 if(myPrev && myPrev.nodeType === Node.ELEMENT_NODE) {
69 ret = myPrev.append(ptr);
79 return {node: ret, offset: ret.sameNode(this) ? null : ret.getText().length - this.getText().length};
84 var undoRedoAction = function(dir) {
88 document: {type: 'context', name: 'document'},
91 label: dir === 'undo' ? '<-' : '->',
93 iconStyle: dir === 'undo' ? '-webkit-transform: scale(-1,1); transform: scale(-1, 1)' : '',
94 execute: function(callback, params) {
95 var metadata = _.last(params.document[dir+'Stack']).metadata,
96 fragment = metadata && metadata.fragment;
97 params.document[dir]();
99 if(!fragment.isValid()) {
100 fragment.restoreFromPaths();
102 if(fragment.isValid()) {
109 getState: function(params) {
110 var allowed = params.document && !!(params.document[dir+'Stack'].length),
111 desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'),
112 descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo');
114 var metadata = _.last(params.document[dir+'Stack']).metadata;
116 desc += ': ' + (metadata.description || gettext('unknown operation'));
121 description: allowed ? desc : descEmpty
127 var pad = function(number) {
129 number = '0' + number;
134 var commentAction = {
137 fragment: {type: 'context', name: 'fragment'}
141 execute: function(callback, params, editor) {
143 var node = params.fragment.node,
145 if(node.nodeType === Node.TEXT_NODE) {
146 node = node.parent();
148 node.document.transaction(function() {
149 var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}});
150 comment.append({text:''});
151 var user = editor.getUser(), creator;
155 creator += ' (' + user.email + ')';
158 creator = 'anonymous';
161 var currentDate = new Date(),
162 dt = pad(currentDate.getDate()) + '-' +
163 pad((currentDate.getMonth() + 1)) + '-' +
164 pad(currentDate.getFullYear()) + ' ' +
165 pad(currentDate.getHours()) + ':' +
166 pad(currentDate.getMinutes()) + ':' +
167 pad(currentDate.getSeconds());
169 var metadata = comment.getMetadata();
170 metadata.add({key: 'creator', value: creator});
171 metadata.add({key: 'date', value: dt});
174 description: action.getState().description
180 getState: function(params) {
182 allowed: params.fragment && params.fragment.isValid() &&
183 params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
186 state.description = gettext('Insert comment');
193 var createWrapTextAction = function(createParams) {
195 name: createParams.name,
197 fragment: {type: 'context', name: 'fragment'},
199 getState: function(params) {
201 label: this.config.label
205 if(!params.fragment || !params.fragment.isValid()) {
206 return _.extend(state, {allowed: false});
209 if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside(createParams.klass)) {
210 return _.extend(state, {
213 description: createParams.unwrapDescription,
214 execute: function(callback, params) {
215 var node = params.fragment.node,
217 toRemove = node.getParent(createParams.klass),
220 if(node.sameNode(toRemove.contents()[0]) && toRemove.isPrecededByTextNode()) {
221 prefLen = toRemove.prev().getText().length;
224 doc.transaction(function() {
225 var ret = toRemove.unwrapContent(),
226 newFragment = params.fragment;
227 if(!newFragment.isValid()) {
228 newFragment = doc.createFragment(doc.CaretFragment, {
230 offset: prefLen + params.fragment.offset
236 description: createParams.unwrapDescription,
237 fragment: params.fragment
245 if(params.fragment instanceof params.fragment.TextRangeFragment && params.fragment.hasSiblingBoundries()) {
246 parent = params.fragment.startNode.parent();
247 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
248 return _.extend(state, {allowed: false});
251 return _.extend(state, {
253 description: createParams.wrapDescription,
254 execute: function(callback, params) {
255 params.fragment.document.transaction(function() {
256 var parent = params.fragment.startNode.parent(),
257 doc = params.fragment.document,
258 wrapper, lastTextNode;
260 wrapper = parent.wrapText({
261 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
262 offsetStart: params.fragment.startOffset,
263 offsetEnd: params.fragment.endOffset,
264 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
267 lastTextNode = wrapper.getLastTextNode();
269 return doc.createFragment(doc.CaretFragment, {node: lastTextNode, offset: lastTextNode.getText().length});
273 description: createParams.wrapDescription,
274 fragment: params.fragment
281 return _.extend(state, {allowed: false});
287 var createLinkFromSelection = function(callback, params) {
288 var doc = params.fragment.document,
289 dialog = Dialog.create({
290 title: gettext('Create link'),
291 executeButtonText: gettext('Apply'),
292 cancelButtonText: gettext('Cancel'),
294 {label: gettext('Link'), name: 'href', type: 'input',
295 prePasteHandler: function(text) {
296 return params.fragment.document.getLinkForUrl(text);
303 dialog.on('execute', function(event) {
304 doc.transaction(function() {
305 var span = action.params.fragment.startNode.parent().wrapText({
306 _with: {tagName: 'span', attrs: {'class': 'link', href: event.formData.href }},
307 offsetStart: params.fragment.startOffset,
308 offsetEnd: params.fragment.endOffset,
309 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
311 doc = params.fragment.document;
313 return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
316 description: action.getState().description,
317 fragment: params.fragment
325 var editLink = function(callback, params) {
326 var doc = params.fragment.document,
327 link = params.fragment.node.getParent('link'),
328 dialog = Dialog.create({
329 title: gettext('Edit link'),
330 executeButtonText: gettext('Apply'),
331 cancelButtonText: gettext('Cancel'),
333 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
338 dialog.on('execute', function(event) {
339 doc.transaction(function() {
340 link.setAttr('href', event.formData.href);
342 return params.fragment;
345 description: action.getState().description,
346 fragment: params.fragment
357 fragment: {type: 'context', name: 'fragment'}
360 label: gettext('link')
362 getState: function(params) {
363 if(!params.fragment || !params.fragment.isValid()) {
364 return {allowed: false};
367 if(params.fragment instanceof params.fragment.TextRangeFragment) {
368 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
369 return {allowed: false};
373 description: gettext('Create link from selection'),
374 execute: createLinkFromSelection
378 if(params.fragment instanceof params.fragment.CaretFragment) {
379 if(params.fragment.node.isInside('link')) {
383 description: gettext('Edit link'),
388 return {allowed: false};
394 undoRedoAction('undo'),
395 undoRedoAction('redo'),
397 createWrapTextAction({name: 'emphasis', klass: 'emp', wrapDescription: gettext('Mark as emphasized'), unwrapDescription: gettext('Remove emphasis')}),
398 createWrapTextAction({name: 'cite', klass: 'cite', wrapDescription: gettext('Mark as citation'), unwrapDescription: gettext('Remove citation')}),
400 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
404 plugin.config = function(config) {
405 // templates.actions[0].config(config.templates);
406 templates.actions[0].params.template.options = config.templates;
409 plugin.canvasElements = canvasElements;