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,
224 fragment: params.fragment
232 if(params.fragment instanceof params.fragment.TextRangeFragment && params.fragment.hasSiblingBoundries()) {
233 parent = params.fragment.startNode.parent();
234 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
235 return _.extend(state, {allowed: false});
238 return _.extend(state, {
240 description: createParams.wrapDescription,
241 execute: function(callback, params) {
242 params.fragment.document.transaction(function() {
243 var parent = params.fragment.startNode.parent(),
244 doc = params.fragment.document,
245 wrapper, lastTextNode;
247 wrapper = parent.wrapText({
248 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
249 offsetStart: params.fragment.startOffset,
250 offsetEnd: params.fragment.endOffset,
251 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
254 lastTextNode = wrapper.getLastTextNode();
256 return doc.createFragment(doc.CaretFragment, {node: lastTextNode, offset: lastTextNode.getText().length});
260 description: createParams.wrapDescription,
261 fragment: params.fragment
268 return _.extend(state, {allowed: false});
274 var createLinkFromSelection = function(callback, params) {
275 var doc = params.fragment.document,
276 dialog = Dialog.create({
277 title: gettext('Create link'),
278 executeButtonText: gettext('Apply'),
279 cancelButtonText: gettext('Cancel'),
281 {label: gettext('Link'), name: 'href', type: 'input',
282 prePasteHandler: function(text) {
283 return params.fragment.document.getLinkForUrl(text);
290 dialog.on('execute', function(event) {
291 doc.transaction(function() {
292 var span = action.params.fragment.startNode.parent().wrapText({
293 _with: {tagName: 'span', attrs: {'class': 'link', href: event.formData.href }},
294 offsetStart: params.fragment.startOffset,
295 offsetEnd: params.fragment.endOffset,
296 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
298 doc = params.fragment.document;
300 return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
303 description: action.getState().description,
304 fragment: params.fragment
312 var editLink = function(callback, params) {
313 var doc = params.fragment.document,
314 link = params.fragment.node.getParent('link'),
315 dialog = Dialog.create({
316 title: gettext('Edit link'),
317 executeButtonText: gettext('Apply'),
318 cancelButtonText: gettext('Cancel'),
320 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
325 dialog.on('execute', function(event) {
326 doc.transaction(function() {
327 link.setAttr('href', event.formData.href);
329 return params.fragment;
332 description: action.getState().description,
333 fragment: params.fragment
344 fragment: {type: 'context', name: 'fragment'}
347 label: gettext('link')
349 getState: function(params) {
350 if(!params.fragment || !params.fragment.isValid()) {
351 return {allowed: false};
354 if(params.fragment instanceof params.fragment.TextRangeFragment) {
355 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
356 return {allowed: false};
360 description: gettext('Create link from selection'),
361 execute: createLinkFromSelection
365 if(params.fragment instanceof params.fragment.CaretFragment) {
366 if(params.fragment.node.isInside('link')) {
370 description: gettext('Edit link'),
375 return {allowed: false};
381 undoRedoAction('undo'),
382 undoRedoAction('redo'),
384 createWrapTextAction({name: 'emphasis', klass: 'emp', wrapDescription: gettext('Mark as emphasized'), unwrapDescription: gettext('Remove emphasis')}),
385 createWrapTextAction({name: 'cite', klass: 'cite', wrapDescription: gettext('Mark as citation'), unwrapDescription: gettext('Remove citation')}),
387 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
391 plugin.config = function(config) {
392 // templates.actions[0].config(config.templates);
393 templates.actions[0].params.template.options = config.templates;
396 plugin.canvasElements = canvasElements;