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) {
22 newNodes = node.split({offset: args.offset});
23 [newNodes.first, newNodes.second].some(function(newNode) {
24 if(!(newNode.contents().length)) {
25 emptyText = newNode.append({text: ''});
29 newNodes.second.contents()
30 .filter(function(child) {
31 return child.object.describesParent;
33 .forEach(function(child) {
35 newNodes.first.append(child);
37 return _.extend(newNodes, {emptyText: emptyText});
39 getChangeRoot: function() {
40 return this.context.parent().parent();
43 mergeContentUp: function() {
49 if(myPrev.nodeType === Node.TEXT_NODE) {
50 if(myPrev.getIndex() > 0) {
53 myPrev = base = myPrev.parent();
56 myPrev = myPrev && myPrev.prev();
58 if(myPrev && myPrev.nodeType === Node.ELEMENT_NODE) {
64 ret = myPrev.append(ptr);
74 return {node: ret, offset: ret.sameNode(this) ? null : ret.getText().length - this.getText().length};
79 var undoRedoAction = function(dir) {
83 document: {type: 'context', name: 'document'},
86 label: dir === 'undo' ? '<-' : '->',
88 iconStyle: dir === 'undo' ? '-webkit-transform: scale(-1,1); transform: scale(-1, 1)' : '',
89 execute: function(callback, params) {
90 var metadata = _.last(params.document[dir+'Stack']).metadata,
91 fragment = metadata && metadata.fragment;
92 params.document[dir]();
94 if(!fragment.isValid()) {
95 fragment.restoreFromPaths();
97 if(fragment.isValid()) {
104 getState: function(params) {
105 var allowed = params.document && !!(params.document[dir+'Stack'].length),
106 desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'),
107 descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo');
109 var metadata = _.last(params.document[dir+'Stack']).metadata;
111 desc += ': ' + (metadata.description || gettext('unknown operation'));
116 description: allowed ? desc : descEmpty
122 var pad = function(number) {
124 number = '0' + number;
129 var commentAction = {
132 fragment: {type: 'context', name: 'fragment'}
136 execute: function(callback, params, editor) {
138 var node = params.fragment.node,
140 if(node.nodeType === Node.TEXT_NODE) {
141 node = node.parent();
143 node.document.transaction(function() {
144 var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}});
145 comment.append({text:''});
146 var user = editor.getUser(), creator;
150 creator += ' (' + user.email + ')';
153 creator = 'anonymous';
156 var currentDate = new Date(),
157 dt = pad(currentDate.getDate()) + '-' +
158 pad((currentDate.getMonth() + 1)) + '-' +
159 pad(currentDate.getFullYear()) + ' ' +
160 pad(currentDate.getHours()) + ':' +
161 pad(currentDate.getMinutes()) + ':' +
162 pad(currentDate.getSeconds());
164 var metadata = comment.getMetadata();
165 metadata.add({key: 'creator', value: creator});
166 metadata.add({key: 'date', value: dt});
169 description: action.getState().description
175 getState: function(params) {
177 allowed: params.fragment && params.fragment.isValid() &&
178 params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
181 state.description = gettext('Insert comment');
188 var createWrapTextAction = function(createParams) {
190 name: createParams.name,
192 fragment: {type: 'context', name: 'fragment'},
194 getState: function(params) {
196 label: this.config.label
200 if(!params.fragment || !params.fragment.isValid()) {
201 return _.extend(state, {allowed: false});
204 if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside(createParams.klass)) {
205 return _.extend(state, {
208 description: createParams.unwrapDescription,
209 execute: function(callback, params) {
210 var node = params.fragment.node,
212 toRemove = node.getParent(createParams.klass),
215 if(node.sameNode(toRemove.contents()[0]) && toRemove.isPrecededByTextNode()) {
216 prefLen = toRemove.prev().getText().length;
219 doc.transaction(function() {
220 var ret = toRemove.unwrapContent(),
221 newFragment = params.fragment;
222 if(!newFragment.isValid()) {
223 newFragment = doc.createFragment(doc.CaretFragment, {
225 offset: prefLen + params.fragment.offset
231 description: createParams.unwrapDescription,
232 fragment: params.fragment
240 if(params.fragment instanceof params.fragment.TextRangeFragment && params.fragment.hasSiblingBoundries()) {
241 parent = params.fragment.startNode.parent();
242 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
243 return _.extend(state, {allowed: false});
246 return _.extend(state, {
248 description: createParams.wrapDescription,
249 execute: function(callback, params) {
250 params.fragment.document.transaction(function() {
251 var parent = params.fragment.startNode.parent(),
252 doc = params.fragment.document,
253 wrapper, lastTextNode;
255 wrapper = parent.wrapText({
256 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
257 offsetStart: params.fragment.startOffset,
258 offsetEnd: params.fragment.endOffset,
259 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
262 lastTextNode = wrapper.getLastTextNode();
264 return doc.createFragment(doc.CaretFragment, {node: lastTextNode, offset: lastTextNode.getText().length});
268 description: createParams.wrapDescription,
269 fragment: params.fragment
276 return _.extend(state, {allowed: false});
282 var createLinkFromSelection = function(callback, params) {
283 var doc = params.fragment.document,
284 dialog = Dialog.create({
285 title: gettext('Create link'),
286 executeButtonText: gettext('Apply'),
287 cancelButtonText: gettext('Cancel'),
289 {label: gettext('Link'), name: 'href', type: 'input',
290 prePasteHandler: function(text) {
291 return params.fragment.document.getLinkForUrl(text);
298 dialog.on('execute', function(event) {
299 doc.transaction(function() {
300 var span = action.params.fragment.startNode.parent().wrapText({
301 _with: {tagName: 'span', attrs: {'class': 'link', href: event.formData.href }},
302 offsetStart: params.fragment.startOffset,
303 offsetEnd: params.fragment.endOffset,
304 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
306 doc = params.fragment.document;
308 return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
311 description: action.getState().description,
312 fragment: params.fragment
320 var editLink = function(callback, params) {
321 var doc = params.fragment.document,
322 link = params.fragment.node.getParent('link'),
323 dialog = Dialog.create({
324 title: gettext('Edit link'),
325 executeButtonText: gettext('Apply'),
326 cancelButtonText: gettext('Cancel'),
328 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
333 dialog.on('execute', function(event) {
334 doc.transaction(function() {
335 link.setAttr('href', event.formData.href);
337 return params.fragment;
340 description: action.getState().description,
341 fragment: params.fragment
352 fragment: {type: 'context', name: 'fragment'}
355 label: gettext('link')
357 getState: function(params) {
358 if(!params.fragment || !params.fragment.isValid()) {
359 return {allowed: false};
362 if(params.fragment instanceof params.fragment.TextRangeFragment) {
363 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
364 return {allowed: false};
368 description: gettext('Create link from selection'),
369 execute: createLinkFromSelection
373 if(params.fragment instanceof params.fragment.CaretFragment) {
374 if(params.fragment.node.isInside('link')) {
378 description: gettext('Edit link'),
383 return {allowed: false};
389 undoRedoAction('undo'),
390 undoRedoAction('redo'),
392 createWrapTextAction({name: 'emphasis', klass: 'emp', wrapDescription: gettext('Mark as emphasized'), unwrapDescription: gettext('Remove emphasis')}),
393 createWrapTextAction({name: 'cite', klass: 'cite', wrapDescription: gettext('Mark as citation'), unwrapDescription: gettext('Remove citation')}),
395 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
399 plugin.config = function(config) {
400 // templates.actions[0].config(config.templates);
401 templates.actions[0].params.template.options = config.templates;
404 plugin.canvasElements = canvasElements;