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 return _.extend(newNodes, {emptyText: emptyText});
31 getChangeRoot: function() {
32 return this.context.parent().parent();
35 mergeContentUp: function() {
41 if(myPrev.nodeType === Node.TEXT_NODE) {
42 if(myPrev.getIndex() > 0) {
45 myPrev = base = myPrev.parent();
48 myPrev = myPrev && myPrev.prev();
50 if(myPrev && myPrev.nodeType === Node.ELEMENT_NODE) {
56 ret = myPrev.append(ptr);
66 return {node: ret, offset: ret.sameNode(this) ? null : ret.getText().length - this.getText().length};
71 var undoRedoAction = function(dir) {
75 document: {type: 'context', name: 'document'},
78 label: dir === 'undo' ? '<-' : '->',
80 iconStyle: dir === 'undo' ? '-webkit-transform: scale(-1,1); transform: scale(-1, 1)' : '',
81 execute: function(callback, params) {
82 var metadata = _.last(params.document[dir+'Stack']).metadata,
83 fragment = metadata && metadata.fragment;
84 params.document[dir]();
86 if(!fragment.isValid()) {
87 fragment.restoreFromPaths();
89 if(fragment.isValid()) {
96 getState: function(params) {
97 var allowed = params.document && !!(params.document[dir+'Stack'].length),
98 desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'),
99 descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo');
101 var metadata = _.last(params.document[dir+'Stack']).metadata;
103 desc += ': ' + (metadata.description || gettext('unknown operation'));
108 description: allowed ? desc : descEmpty
114 var pad = function(number) {
116 number = '0' + number;
121 var commentAction = {
124 fragment: {type: 'context', name: 'fragment'}
128 execute: function(callback, params, editor) {
130 var node = params.fragment.node,
132 if(node.nodeType === Node.TEXT_NODE) {
133 node = node.parent();
135 node.document.transaction(function() {
136 var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}});
137 comment.append({text:''});
138 var user = editor.getUser(), creator;
142 creator += ' (' + user.email + ')';
145 creator = 'anonymous';
148 var currentDate = new Date(),
149 dt = pad(currentDate.getDate()) + '-' +
150 pad((currentDate.getMonth() + 1)) + '-' +
151 pad(currentDate.getFullYear()) + ' ' +
152 pad(currentDate.getHours()) + ':' +
153 pad(currentDate.getMinutes()) + ':' +
154 pad(currentDate.getSeconds());
156 var metadata = comment.getMetadata();
157 metadata.add({key: 'creator', value: creator});
158 metadata.add({key: 'date', value: dt});
161 description: action.getState().description
167 getState: function(params) {
169 allowed: params.fragment && params.fragment.isValid() &&
170 params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
173 state.description = gettext('Insert comment');
180 var createWrapTextAction = function(createParams) {
182 name: createParams.name,
184 fragment: {type: 'context', name: 'fragment'},
186 getState: function(params) {
188 label: this.config.label
192 if(!params.fragment || !params.fragment.isValid()) {
193 return _.extend(state, {allowed: false});
196 if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside(createParams.klass)) {
197 return _.extend(state, {
200 description: createParams.unwrapDescription,
201 execute: function(callback, params) {
202 var node = params.fragment.node,
204 toRemove = node.getParent(createParams.klass),
207 if(node.sameNode(toRemove.contents()[0]) && toRemove.isPrecededByTextNode()) {
208 prefLen = toRemove.prev().getText().length;
211 doc.transaction(function() {
212 var ret = toRemove.unwrapContent(),
213 newFragment = params.fragment;
214 if(!newFragment.isValid()) {
215 newFragment = doc.createFragment(doc.CaretFragment, {
217 offset: prefLen + params.fragment.offset
223 description: createParams.unwrapDescription
231 if(params.fragment instanceof params.fragment.TextRangeFragment && params.fragment.hasSiblingBoundries()) {
232 parent = params.fragment.startNode.parent();
233 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
234 return _.extend(state, {allowed: false});
237 return _.extend(state, {
239 description: createParams.wrapDescription,
240 execute: function(callback, params) {
241 params.fragment.document.transaction(function() {
242 var parent = params.fragment.startNode.parent(),
243 doc = params.fragment.document,
244 wrapper, lastTextNode;
246 wrapper = parent.wrapText({
247 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
248 offsetStart: params.fragment.startOffset,
249 offsetEnd: params.fragment.endOffset,
250 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
253 lastTextNode = wrapper.getLastTextNode();
255 return doc.createFragment(doc.CaretFragment, {node: lastTextNode, offset: lastTextNode.getText().length});
259 description: createParams.wrapDescription
266 return _.extend(state, {allowed: false});
272 var createLinkFromSelection = function(callback, params) {
273 var doc = params.fragment.document,
274 dialog = Dialog.create({
275 title: gettext('Create link'),
276 executeButtonText: gettext('Apply'),
277 cancelButtonText: gettext('Cancel'),
279 {label: gettext('Link'), name: 'href', type: 'input',
280 prePasteHandler: function(text) {
281 return params.fragment.document.getLinkForUrl(text);
288 dialog.on('execute', function(event) {
289 doc.transaction(function() {
290 var span = action.params.fragment.startNode.parent().wrapText({
291 _with: {tagName: 'span', attrs: {'class': 'link', href: event.formData.href }},
292 offsetStart: params.fragment.startOffset,
293 offsetEnd: params.fragment.endOffset,
294 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
296 doc = params.fragment.document;
298 return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
301 description: action.getState().description
309 var editLink = function(callback, params) {
310 var doc = params.fragment.document,
311 link = params.fragment.node.getParent('link'),
312 dialog = Dialog.create({
313 title: gettext('Edit link'),
314 executeButtonText: gettext('Apply'),
315 cancelButtonText: gettext('Cancel'),
317 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
322 dialog.on('execute', function(event) {
323 doc.transaction(function() {
324 link.setAttr('href', event.formData.href);
326 return params.fragment;
329 description: action.getState().description
340 fragment: {type: 'context', name: 'fragment'}
343 label: gettext('link')
345 getState: function(params) {
346 if(!params.fragment || !params.fragment.isValid()) {
347 return {allowed: false};
350 if(params.fragment instanceof params.fragment.TextRangeFragment) {
351 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
352 return {allowed: false};
356 description: gettext('Create link from selection'),
357 execute: createLinkFromSelection
361 if(params.fragment instanceof params.fragment.CaretFragment) {
362 if(params.fragment.node.isInside('link')) {
366 description: gettext('Edit link'),
371 return {allowed: false};
377 undoRedoAction('undo'),
378 undoRedoAction('redo'),
380 createWrapTextAction({name: 'emphasis', klass: 'emp', wrapDescription: gettext('Mark as emphasized'), unwrapDescription: gettext('Remove emphasis')}),
381 createWrapTextAction({name: 'cite', klass: 'cite', wrapDescription: gettext('Mark as citation'), unwrapDescription: gettext('Remove citation')}),
383 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
387 plugin.config = function(config) {
388 // templates.actions[0].config(config.templates);
389 templates.actions[0].params.template.options = config.templates;
392 plugin.canvasElements = canvasElements;