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'),
14 metadataEditor = require('plugins/core/metadataEditor/metadataEditor');
18 plugin.documentExtension.textNode.transformations = {
20 impl: function(args) {
22 parentDescribingNodes = [],
24 newNodes = node.split({offset: args.offset});
25 newNodes.second.contents()
26 .filter(function(child) {
27 return child.object.describesParent;
29 .forEach(function(child) {
31 parentDescribingNodes.push(child);
34 [newNodes.first, newNodes.second].some(function(newNode) {
35 if(!(newNode.contents().length)) {
36 emptyText = newNode.append({text: ''});
40 parentDescribingNodes.forEach(function(node) {
41 newNodes.first.append(node);
43 return _.extend(newNodes, {emptyText: emptyText});
45 getChangeRoot: function() {
46 return this.context.parent().parent();
49 mergeContentUp: function() {
55 if(myPrev.nodeType === Node.TEXT_NODE) {
56 if(myPrev.getIndex() > 0) {
59 myPrev = base = myPrev.parent();
62 myPrev = myPrev && myPrev.prev();
64 if(myPrev && myPrev.nodeType === Node.ELEMENT_NODE) {
70 ret = myPrev.append(ptr);
80 return {node: ret, offset: ret.sameNode(this) ? null : ret.getText().length - this.getText().length};
85 var undoRedoAction = function(dir) {
89 document: {type: 'context', name: 'document'},
92 label: dir === 'undo' ? '<-' : '->',
94 iconStyle: dir === 'undo' ? '-webkit-transform: scale(-1,1); transform: scale(-1, 1)' : '',
95 execute: function(callback, params) {
96 var metadata = _.last(params.document[dir+'Stack']).metadata,
97 fragment = metadata && metadata.fragment;
98 params.document[dir]();
100 if(!fragment.isValid()) {
101 fragment.restoreFromPaths();
103 if(fragment.isValid()) {
110 getState: function(params) {
111 var allowed = params.document && !!(params.document[dir+'Stack'].length),
112 desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'),
113 descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo');
115 var metadata = _.last(params.document[dir+'Stack']).metadata;
117 desc += ': ' + (metadata.description || gettext('unknown operation'));
122 description: allowed ? desc : descEmpty
128 var pad = function(number) {
130 number = '0' + number;
135 var commentAction = {
138 fragment: {type: 'context', name: 'fragment'}
142 execute: function(callback, params, editor) {
144 var node = params.fragment.node,
146 if(node.nodeType === Node.TEXT_NODE) {
147 node = node.parent();
149 node.document.transaction(function() {
150 var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}});
151 comment.append({text:''});
152 var user = editor.getUser(), creator;
156 creator += ' (' + user.email + ')';
159 creator = 'anonymous';
162 var currentDate = new Date(),
163 dt = pad(currentDate.getDate()) + '-' +
164 pad((currentDate.getMonth() + 1)) + '-' +
165 pad(currentDate.getFullYear()) + ' ' +
166 pad(currentDate.getHours()) + ':' +
167 pad(currentDate.getMinutes()) + ':' +
168 pad(currentDate.getSeconds());
170 var metadata = comment.getMetadata();
171 metadata.add({key: 'creator', value: creator});
172 metadata.add({key: 'date', value: dt});
175 description: action.getState().description
181 getState: function(params) {
183 allowed: params.fragment && params.fragment.isValid() &&
184 params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
187 state.description = gettext('Insert comment');
194 var createWrapTextAction = function(createParams) {
196 name: createParams.name,
198 fragment: {type: 'context', name: 'fragment'},
200 getState: function(params) {
202 label: this.config.label
206 if(!params.fragment || !params.fragment.isValid()) {
207 return _.extend(state, {allowed: false});
210 if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside(createParams.klass)) {
211 return _.extend(state, {
214 description: createParams.unwrapDescription,
215 execute: function(callback, params) {
216 var node = params.fragment.node,
218 toRemove = node.getParent(createParams.klass),
221 if(node.sameNode(toRemove.contents()[0]) && toRemove.isPrecededByTextNode()) {
222 prefLen = toRemove.prev().getText().length;
225 doc.transaction(function() {
226 var ret = toRemove.unwrapContent(),
227 newFragment = params.fragment;
228 if(!newFragment.isValid()) {
229 newFragment = doc.createFragment(doc.CaretFragment, {
231 offset: prefLen + params.fragment.offset
237 description: createParams.unwrapDescription,
238 fragment: params.fragment
246 if(params.fragment instanceof params.fragment.TextRangeFragment && params.fragment.hasSiblingBoundries()) {
247 parent = params.fragment.startNode.parent();
248 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
249 return _.extend(state, {allowed: false});
252 return _.extend(state, {
254 description: createParams.wrapDescription,
255 execute: function(callback, params) {
256 params.fragment.document.transaction(function() {
257 var parent = params.fragment.startNode.parent(),
258 doc = params.fragment.document,
259 wrapper, lastTextNode;
261 wrapper = parent.wrapText({
262 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
263 offsetStart: params.fragment.startOffset,
264 offsetEnd: params.fragment.endOffset,
265 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
268 lastTextNode = wrapper.getLastTextNode();
270 return doc.createFragment(doc.CaretFragment, {node: lastTextNode, offset: lastTextNode.getText().length});
274 description: createParams.wrapDescription,
275 fragment: params.fragment
282 return _.extend(state, {allowed: false});
288 var createLinkFromSelection = function(callback, params) {
289 var doc = params.fragment.document,
290 dialog = Dialog.create({
291 title: gettext('Create link'),
292 executeButtonText: gettext('Apply'),
293 cancelButtonText: gettext('Cancel'),
295 {label: gettext('Link'), name: 'href', type: 'input',
296 prePasteHandler: function(text) {
297 return params.fragment.document.getLinkForUrl(text);
304 dialog.on('execute', function(event) {
305 doc.transaction(function() {
306 var span = action.params.fragment.startNode.parent().wrapText({
307 _with: {tagName: 'span', attrs: {'class': 'link', href: event.formData.href }},
308 offsetStart: params.fragment.startOffset,
309 offsetEnd: params.fragment.endOffset,
310 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
312 doc = params.fragment.document;
314 return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
317 description: action.getState().description,
318 fragment: params.fragment
326 var editLink = function(callback, params) {
327 var doc = params.fragment.document,
328 link = params.fragment.node.getParent('link'),
329 dialog = Dialog.create({
330 title: gettext('Edit link'),
331 executeButtonText: gettext('Apply'),
332 cancelButtonText: gettext('Cancel'),
334 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
339 dialog.on('execute', function(event) {
340 doc.transaction(function() {
341 link.setAttr('href', event.formData.href);
343 return params.fragment;
346 description: action.getState().description,
347 fragment: params.fragment
358 fragment: {type: 'context', name: 'fragment'}
361 label: gettext('link')
363 getState: function(params) {
364 if(!params.fragment || !params.fragment.isValid()) {
365 return {allowed: false};
368 if(params.fragment instanceof params.fragment.TextRangeFragment) {
369 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
370 return {allowed: false};
374 description: gettext('Create link from selection'),
375 execute: createLinkFromSelection
379 if(params.fragment instanceof params.fragment.CaretFragment) {
380 if(params.fragment.node.isInside('link')) {
384 description: gettext('Edit link'),
389 return {allowed: false};
393 var metadataParams = {};
396 undoRedoAction('undo'),
397 undoRedoAction('redo'),
399 createWrapTextAction({name: 'emphasis', klass: 'emp', wrapDescription: gettext('Mark as emphasized'), unwrapDescription: gettext('Remove emphasis')}),
400 createWrapTextAction({name: 'cite', klass: 'cite', wrapDescription: gettext('Mark as citation'), unwrapDescription: gettext('Remove citation')}),
402 metadataEditor.action(metadataParams)
403 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
407 plugin.config = function(config) {
408 // templates.actions[0].config(config.templates);
409 templates.actions[0].params.template.options = config.templates;
410 metadataParams.config = (config.metadata || []).sort(function(configRow1, configRow2) {
411 if(configRow1.key < configRow2.key) {
414 if(configRow1.key > configRow2.key) {
421 plugin.canvasElements = canvasElements;