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');
15 plugin.documentExtension.textNode.transformations = {
17 impl: function(args) {
20 newNodes = node.split({offset: args.offset});
21 [newNodes.first, newNodes.second].some(function(newNode) {
22 if(!(newNode.contents().length)) {
23 emptyText = newNode.append({text: ''});
27 return _.extend(newNodes, {emptyText: emptyText});
29 getChangeRoot: function() {
30 return this.context.parent().parent();
33 mergeContentUp: function() {
39 if(myPrev.nodeType === Node.TEXT_NODE) {
40 if(myPrev.getIndex() > 0) {
43 myPrev = base = myPrev.parent();
46 myPrev = myPrev && myPrev.prev();
48 if(myPrev && myPrev.nodeType === Node.ELEMENT_NODE) {
54 ret = myPrev.append(ptr);
64 return {node: ret, offset: ret.sameNode(this) ? null : ret.getText().length - this.getText().length};
69 var undoRedoAction = function(dir) {
73 document: {type: 'context', name: 'document'},
76 label: dir === 'undo' ? '<-' : '->',
78 iconStyle: dir === 'undo' ? '-webkit-transform: scale(-1,1); transform: scale(-1, 1)' : '',
79 execute: function(callback, params) {
80 params.document[dir]();
84 getState: function(params) {
85 var allowed = params.document && !!(params.document[dir+'Stack'].length),
86 desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'),
87 descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo');
89 var metadata = _.last(params.document[dir+'Stack']).metadata;
91 desc += ': ' + (metadata.description || gettext('unknown operation'));
96 description: allowed ? desc : descEmpty
102 var pad = function(number) {
104 number = '0' + number;
109 var commentAction = {
112 fragment: {type: 'context', name: 'fragment'}
116 execute: function(callback, params, editor) {
118 var node = params.fragment.node,
120 if(node.nodeType === Node.TEXT_NODE) {
121 node = node.parent();
123 node.document.transaction(function() {
124 var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}});
125 comment.append({text:''});
126 var user = editor.getUser(), creator;
130 creator += ' (' + user.email + ')';
133 creator = 'anonymous';
136 var currentDate = new Date(),
137 dt = pad(currentDate.getDate()) + '-' +
138 pad((currentDate.getMonth() + 1)) + '-' +
139 pad(currentDate.getFullYear()) + ' ' +
140 pad(currentDate.getHours()) + ':' +
141 pad(currentDate.getMinutes()) + ':' +
142 pad(currentDate.getSeconds());
144 var metadata = comment.getMetadata();
145 metadata.add({key: 'creator', value: creator});
146 metadata.add({key: 'date', value: dt});
149 description: action.getState().description
155 getState: function(params) {
157 allowed: params.fragment && params.fragment.isValid() &&
158 params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
161 state.description = gettext('Insert comment');
168 var createWrapTextAction = function(createParams) {
170 name: createParams.name,
172 fragment: {type: 'context', name: 'fragment'},
174 getState: function(params) {
176 label: this.config.label
180 if(!params.fragment || !params.fragment.isValid()) {
181 return _.extend(state, {allowed: false});
184 if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside(createParams.klass)) {
185 return _.extend(state, {
188 description: createParams.unwrapDescription,
189 execute: function(callback, params) {
190 var node = params.fragment.node,
192 toRemove = node.getParent(createParams.klass),
195 if(node.sameNode(toRemove.contents()[0]) && toRemove.isPrecededByTextNode()) {
196 prefLen = toRemove.prev().getText().length;
199 doc.transaction(function() {
200 var ret = toRemove.unwrapContent(),
201 newFragment = params.fragment;
202 if(!newFragment.isValid()) {
203 newFragment = doc.createFragment(doc.CaretFragment, {
205 offset: prefLen + params.fragment.offset
211 description: createParams.unwrapDescription
219 if(params.fragment instanceof params.fragment.TextRangeFragment && params.fragment.hasSiblingBoundries()) {
220 parent = params.fragment.startNode.parent();
221 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
222 return _.extend(state, {allowed: false});
225 return _.extend(state, {
227 description: createParams.wrapDescription,
228 execute: function(callback, params) {
229 params.fragment.document.transaction(function() {
230 var parent = params.fragment.startNode.parent(),
231 doc = params.fragment.document,
232 wrapper, lastTextNode;
234 wrapper = parent.wrapText({
235 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
236 offsetStart: params.fragment.startOffset,
237 offsetEnd: params.fragment.endOffset,
238 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
241 lastTextNode = wrapper.getLastTextNode();
243 return doc.createFragment(doc.CaretFragment, {node: lastTextNode, offset: lastTextNode.getText().length});
247 description: createParams.wrapDescription
254 return _.extend(state, {allowed: false});
260 var createLinkFromSelection = function(callback, params) {
261 var doc = params.fragment.document,
262 dialog = Dialog.create({
263 title: gettext('Create link'),
264 executeButtonText: gettext('Apply'),
265 cancelButtonText: gettext('Cancel'),
267 {label: gettext('Link'), name: 'href', type: 'input'}
272 dialog.on('execute', function(event) {
273 doc.transaction(function() {
274 var span = action.params.fragment.startNode.parent().wrapText({
275 _with: {tagName: 'span', attrs: {'class': 'link'}},
276 offsetStart: params.fragment.startOffset,
277 offsetEnd: params.fragment.endOffset,
278 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
280 doc = params.fragment.document;
281 span.setAttr('href', event.formData.href);
283 return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
286 description: action.getState().description
294 var editLink = function(callback, params) {
295 var doc = params.fragment.document,
296 link = params.fragment.node.getParent('link'),
297 dialog = Dialog.create({
298 title: gettext('Edit link'),
299 executeButtonText: gettext('Apply'),
300 cancelButtonText: gettext('Cancel'),
302 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
307 dialog.on('execute', function(event) {
308 doc.transaction(function() {
309 link.setAttr('href', event.formData.href);
311 return params.fragment;
314 description: action.getState().description
325 fragment: {type: 'context', name: 'fragment'}
328 label: gettext('link')
330 getState: function(params) {
331 if(!params.fragment || !params.fragment.isValid()) {
332 return {allowed: false};
335 if(params.fragment instanceof params.fragment.TextRangeFragment) {
336 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
337 return {allowed: false};
341 description: gettext('Create link from selection'),
342 execute: createLinkFromSelection
346 if(params.fragment instanceof params.fragment.CaretFragment) {
347 if(params.fragment.node.isInside('link')) {
351 description: gettext('Edit link'),
356 return {allowed: false};
362 undoRedoAction('undo'),
363 undoRedoAction('redo'),
365 createWrapTextAction({name: 'emphasis', klass: 'emp', wrapDescription: gettext('Mark as emphasized'), unwrapDescription: gettext('Remove emphasis')}),
366 createWrapTextAction({name: 'cite', klass: 'cite', wrapDescription: gettext('Mark as citation'), unwrapDescription: gettext('Remove citation')}),
368 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
372 plugin.config = function(config) {
373 // templates.actions[0].config(config.templates);
374 templates.actions[0].params.template.options = config.templates;