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 params.document[dir]();
86 getState: function(params) {
87 var allowed = params.document && !!(params.document[dir+'Stack'].length),
88 desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'),
89 descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo');
91 var metadata = _.last(params.document[dir+'Stack']).metadata;
93 desc += ': ' + (metadata.description || gettext('unknown operation'));
98 description: allowed ? desc : descEmpty
104 var pad = function(number) {
106 number = '0' + number;
111 var commentAction = {
114 fragment: {type: 'context', name: 'fragment'}
118 execute: function(callback, params, editor) {
120 var node = params.fragment.node,
122 if(node.nodeType === Node.TEXT_NODE) {
123 node = node.parent();
125 node.document.transaction(function() {
126 var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}});
127 comment.append({text:''});
128 var user = editor.getUser(), creator;
132 creator += ' (' + user.email + ')';
135 creator = 'anonymous';
138 var currentDate = new Date(),
139 dt = pad(currentDate.getDate()) + '-' +
140 pad((currentDate.getMonth() + 1)) + '-' +
141 pad(currentDate.getFullYear()) + ' ' +
142 pad(currentDate.getHours()) + ':' +
143 pad(currentDate.getMinutes()) + ':' +
144 pad(currentDate.getSeconds());
146 var metadata = comment.getMetadata();
147 metadata.add({key: 'creator', value: creator});
148 metadata.add({key: 'date', value: dt});
151 description: action.getState().description
157 getState: function(params) {
159 allowed: params.fragment && params.fragment.isValid() &&
160 params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
163 state.description = gettext('Insert comment');
170 var createWrapTextAction = function(createParams) {
172 name: createParams.name,
174 fragment: {type: 'context', name: 'fragment'},
176 getState: function(params) {
178 label: this.config.label
182 if(!params.fragment || !params.fragment.isValid()) {
183 return _.extend(state, {allowed: false});
186 if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside(createParams.klass)) {
187 return _.extend(state, {
190 description: createParams.unwrapDescription,
191 execute: function(callback, params) {
192 var node = params.fragment.node,
194 toRemove = node.getParent(createParams.klass),
197 if(node.sameNode(toRemove.contents()[0]) && toRemove.isPrecededByTextNode()) {
198 prefLen = toRemove.prev().getText().length;
201 doc.transaction(function() {
202 var ret = toRemove.unwrapContent(),
203 newFragment = params.fragment;
204 if(!newFragment.isValid()) {
205 newFragment = doc.createFragment(doc.CaretFragment, {
207 offset: prefLen + params.fragment.offset
213 description: createParams.unwrapDescription
221 if(params.fragment instanceof params.fragment.TextRangeFragment && params.fragment.hasSiblingBoundries()) {
222 parent = params.fragment.startNode.parent();
223 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
224 return _.extend(state, {allowed: false});
227 return _.extend(state, {
229 description: createParams.wrapDescription,
230 execute: function(callback, params) {
231 params.fragment.document.transaction(function() {
232 var parent = params.fragment.startNode.parent(),
233 doc = params.fragment.document,
234 wrapper, lastTextNode;
236 wrapper = parent.wrapText({
237 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
238 offsetStart: params.fragment.startOffset,
239 offsetEnd: params.fragment.endOffset,
240 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
243 lastTextNode = wrapper.getLastTextNode();
245 return doc.createFragment(doc.CaretFragment, {node: lastTextNode, offset: lastTextNode.getText().length});
249 description: createParams.wrapDescription
256 return _.extend(state, {allowed: false});
262 var createLinkFromSelection = function(callback, params) {
263 var doc = params.fragment.document,
264 dialog = Dialog.create({
265 title: gettext('Create link'),
266 executeButtonText: gettext('Apply'),
267 cancelButtonText: gettext('Cancel'),
269 {label: gettext('Link'), name: 'href', type: 'input'}
274 dialog.on('execute', function(event) {
275 doc.transaction(function() {
276 var span = action.params.fragment.startNode.parent().wrapText({
277 _with: {tagName: 'span', attrs: {'class': 'link', href: event.formData.href }},
278 offsetStart: params.fragment.startOffset,
279 offsetEnd: params.fragment.endOffset,
280 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
282 doc = params.fragment.document;
284 return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
287 description: action.getState().description
295 var editLink = function(callback, params) {
296 var doc = params.fragment.document,
297 link = params.fragment.node.getParent('link'),
298 dialog = Dialog.create({
299 title: gettext('Edit link'),
300 executeButtonText: gettext('Apply'),
301 cancelButtonText: gettext('Cancel'),
303 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
308 dialog.on('execute', function(event) {
309 doc.transaction(function() {
310 link.setAttr('href', event.formData.href);
312 return params.fragment;
315 description: action.getState().description
326 fragment: {type: 'context', name: 'fragment'}
329 label: gettext('link')
331 getState: function(params) {
332 if(!params.fragment || !params.fragment.isValid()) {
333 return {allowed: false};
336 if(params.fragment instanceof params.fragment.TextRangeFragment) {
337 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
338 return {allowed: false};
342 description: gettext('Create link from selection'),
343 execute: createLinkFromSelection
347 if(params.fragment instanceof params.fragment.CaretFragment) {
348 if(params.fragment.node.isInside('link')) {
352 description: gettext('Edit link'),
357 return {allowed: false};
363 undoRedoAction('undo'),
364 undoRedoAction('redo'),
366 createWrapTextAction({name: 'emphasis', klass: 'emp', wrapDescription: gettext('Mark as emphasized'), unwrapDescription: gettext('Remove emphasis')}),
367 createWrapTextAction({name: 'cite', klass: 'cite', wrapDescription: gettext('Mark as citation'), unwrapDescription: gettext('Remove citation')}),
369 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
373 plugin.config = function(config) {
374 // templates.actions[0].config(config.templates);
375 templates.actions[0].params.template.options = config.templates;
378 plugin.canvasElements = canvasElements;