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',
270 prePasteHandler: function(text) {
271 return params.fragment.document.getLinkForUrl(text);
278 dialog.on('execute', function(event) {
279 doc.transaction(function() {
280 var span = action.params.fragment.startNode.parent().wrapText({
281 _with: {tagName: 'span', attrs: {'class': 'link', href: event.formData.href }},
282 offsetStart: params.fragment.startOffset,
283 offsetEnd: params.fragment.endOffset,
284 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
286 doc = params.fragment.document;
288 return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
291 description: action.getState().description
299 var editLink = function(callback, params) {
300 var doc = params.fragment.document,
301 link = params.fragment.node.getParent('link'),
302 dialog = Dialog.create({
303 title: gettext('Edit link'),
304 executeButtonText: gettext('Apply'),
305 cancelButtonText: gettext('Cancel'),
307 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
312 dialog.on('execute', function(event) {
313 doc.transaction(function() {
314 link.setAttr('href', event.formData.href);
316 return params.fragment;
319 description: action.getState().description
330 fragment: {type: 'context', name: 'fragment'}
333 label: gettext('link')
335 getState: function(params) {
336 if(!params.fragment || !params.fragment.isValid()) {
337 return {allowed: false};
340 if(params.fragment instanceof params.fragment.TextRangeFragment) {
341 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
342 return {allowed: false};
346 description: gettext('Create link from selection'),
347 execute: createLinkFromSelection
351 if(params.fragment instanceof params.fragment.CaretFragment) {
352 if(params.fragment.node.isInside('link')) {
356 description: gettext('Edit link'),
361 return {allowed: false};
367 undoRedoAction('undo'),
368 undoRedoAction('redo'),
370 createWrapTextAction({name: 'emphasis', klass: 'emp', wrapDescription: gettext('Mark as emphasized'), unwrapDescription: gettext('Remove emphasis')}),
371 createWrapTextAction({name: 'cite', klass: 'cite', wrapDescription: gettext('Mark as citation'), unwrapDescription: gettext('Remove citation')}),
373 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
377 plugin.config = function(config) {
378 // templates.actions[0].config(config.templates);
379 templates.actions[0].params.template.options = config.templates;
382 plugin.canvasElements = canvasElements;