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() {
34 var myPrev = this.prev(),
38 ret = myPrev.append(this);
39 return {node: ret, offset: ret.sameNode(this) ? null : ret.getText().length - this.getText().length};
41 var range = this.parent().unwrapContent();
42 return {node: range.element1, offset: 0};
47 var undoRedoAction = function(dir) {
51 document: {type: 'context', name: 'document'},
54 label: dir === 'undo' ? '<-' : '->',
56 iconStyle: dir === 'undo' ? '-webkit-transform: scale(-1,1); transform: scale(-1, 1)' : '',
57 execute: function(callback, params) {
58 params.document[dir]();
62 getState: function(params) {
63 var allowed = params.document && !!(params.document[dir+'Stack'].length),
64 desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'),
65 descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo');
67 var metadata = _.last(params.document[dir+'Stack']).metadata;
69 desc += ': ' + (metadata.description || gettext('unknown operation'));
74 description: allowed ? desc : descEmpty
80 var pad = function(number) {
82 number = '0' + number;
90 fragment: {type: 'context', name: 'fragment'}
94 execute: function(callback, params, editor) {
96 var node = params.fragment.node,
98 if(node.nodeType === Node.TEXT_NODE) {
101 node.document.transaction(function() {
102 var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}});
103 comment.append({text:''});
104 var user = editor.getUser(), creator;
108 creator += ' (' + user.email + ')';
111 creator = 'anonymous';
114 var currentDate = new Date(),
115 dt = pad(currentDate.getDate()) + '-' +
116 pad((currentDate.getMonth() + 1)) + '-' +
117 pad(currentDate.getFullYear()) + ' ' +
118 pad(currentDate.getHours()) + ':' +
119 pad(currentDate.getMinutes()) + ':' +
120 pad(currentDate.getSeconds());
122 var metadata = comment.getMetadata();
123 metadata.add({key: 'creator', value: creator});
124 metadata.add({key: 'date', value: dt});
127 description: action.getState().description
133 getState: function(params) {
135 allowed: params.fragment && params.fragment.isValid() &&
136 params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
139 state.description = gettext('Insert comment after current node');
146 var createWrapTextAction = function(createParams) {
148 name: createParams.name,
150 fragment: {type: 'context', name: 'fragment'},
152 getState: function(params) {
154 label: this.config.label
159 !params.fragment || !params.fragment.isValid() ||
160 !(params.fragment instanceof params.fragment.TextRangeFragment) ||
161 !params.fragment.hasSiblingBoundries()) {
162 return _.extend(state, {allowed: false});
165 parent = params.fragment.startNode.parent();
166 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
167 return _.extend(state, {allowed: false});
170 return _.extend(state, {
172 description: createParams.description,
173 execute: function(callback, params) {
174 params.fragment.document.transaction(function() {
175 var parent = params.fragment.startNode.parent();
176 return parent.wrapText({
177 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
178 offsetStart: params.fragment.startOffset,
179 offsetEnd: params.fragment.endOffset,
180 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
184 description: createParams.description
195 var createLinkFromSelection = function(callback, params) {
196 var doc = params.fragment.document,
197 dialog = Dialog.create({
198 title: gettext('Create link'),
199 executeButtonText: gettext('Apply'),
200 cancelButtonText: gettext('Cancel'),
202 {label: gettext('Link'), name: 'href', type: 'input'}
207 dialog.on('execute', function(event) {
208 doc.transaction(function() {
209 var span = action.params.fragment.startNode.parent().wrapText({
210 _with: {tagName: 'span', attrs: {'class': 'link'}},
211 offsetStart: params.fragment.startOffset,
212 offsetEnd: params.fragment.endOffset,
213 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
215 doc = params.fragment.document;
216 span.setAttr('href', event.formData.href);
218 return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
221 description: action.getState().description
229 var editLink = function(callback, params) {
230 var doc = params.fragment.document,
231 link = params.fragment.node.getParent('link'),
232 dialog = Dialog.create({
233 title: gettext('Edit link'),
234 executeButtonText: gettext('Apply'),
235 cancelButtonText: gettext('Cancel'),
237 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
242 dialog.on('execute', function(event) {
243 doc.transaction(function() {
244 link.setAttr('href', event.formData.href);
246 return params.fragment;
249 description: action.getState().description
260 fragment: {type: 'context', name: 'fragment'}
263 label: gettext('link')
265 getState: function(params) {
266 if(!params.fragment || !params.fragment.isValid()) {
267 return {allowed: false};
270 if(params.fragment instanceof params.fragment.TextRangeFragment) {
271 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
272 return {allowed: false};
276 description: gettext('Create link from selection'),
277 execute: createLinkFromSelection
281 if(params.fragment instanceof params.fragment.CaretFragment) {
282 if(params.fragment.node.isInside('link')) {
283 return {allowed: true, toggled: true, execute: editLink};
286 return {allowed: false};
292 undoRedoAction('undo'),
293 undoRedoAction('redo'),
295 createWrapTextAction({name: 'emphasis', klass: 'emp', description: gettext('Mark as emphasized')}),
296 createWrapTextAction({name: 'cite', klass: 'cite', description: gettext('Mark as citation')}),
298 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
302 plugin.config = function(config) {
303 // templates.actions[0].config(config.templates);
304 templates.actions[0].params.template.options = config.templates;