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(params) {
58 params.document[dir]();
61 getState: function(params) {
62 var allowed = params.document && !!(params.document[dir+'Stack'].length),
63 desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'),
64 descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo');
66 var metadata = _.last(params.document[dir+'Stack']).metadata;
68 desc += ': ' + (metadata.description || gettext('unknown operation'));
73 description: allowed ? desc : descEmpty
79 var pad = function(number) {
81 number = '0' + number;
89 fragment: {type: 'context', name: 'fragment'}
93 execute: function(params, editor) {
95 var node = params.fragment.node,
97 if(node.nodeType === Node.TEXT_NODE) {
100 node.document.transaction(function() {
101 var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}});
102 comment.append({text:''});
103 var user = editor.getUser(), creator;
107 creator += ' (' + user.email + ')';
110 creator = 'anonymous';
113 var currentDate = new Date(),
114 dt = pad(currentDate.getDate()) + '-' +
115 pad((currentDate.getMonth() + 1)) + '-' +
116 pad(currentDate.getFullYear()) + ' ' +
117 pad(currentDate.getHours()) + ':' +
118 pad(currentDate.getMinutes()) + ':' +
119 pad(currentDate.getSeconds());
121 var metadata = comment.getMetadata();
122 metadata.add({key: 'creator', value: creator});
123 metadata.add({key: 'date', value: dt});
126 description: action.getState().description
131 getState: function(params) {
133 allowed: params.fragment && params.fragment.isValid() &&
134 params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
137 state.description = gettext('Insert comment after current node');
144 var createWrapTextAction = function(createParams) {
146 name: createParams.name,
148 fragment: {type: 'context', name: 'fragment'},
150 getState: function(params) {
152 label: this.config.label
157 !params.fragment || !params.fragment.isValid() ||
158 !(params.fragment instanceof params.fragment.TextRangeFragment) ||
159 !params.fragment.hasSiblingBoundries()) {
160 return _.extend(state, {allowed: false});
163 parent = params.fragment.startNode.parent();
164 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
165 return _.extend(state, {allowed: false});
168 return _.extend(state, {
170 description: createParams.description,
171 execute: function(params) {
172 params.fragment.document.transaction(function() {
173 var parent = params.fragment.startNode.parent();
174 return parent.wrapText({
175 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
176 offsetStart: params.fragment.startOffset,
177 offsetEnd: params.fragment.endOffset,
178 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
182 description: createParams.description
192 var createLinkFromSelection = function(params) {
193 var doc = params.fragment.document,
194 dialog = Dialog.create({
195 title: gettext('Create link'),
196 executeButtonText: gettext('Apply'),
197 cancelButtonText: gettext('Cancel'),
199 {label: gettext('Link'), name: 'href', type: 'input'}
204 dialog.on('execute', function(event) {
205 doc.transaction(function() {
206 var span = params.fragment.startNode.parent().wrapText({
207 _with: {tagName: 'span', attrs: {'class': 'link'}},
208 offsetStart: params.fragment.startOffset,
209 offsetEnd: params.fragment.endOffset,
210 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
212 span.setAttr('href', event.formData.href);
217 description: action.getState().description
224 var editLink = function(params) {
225 var doc = params.fragment.document,
226 link = params.fragment.node.getParent('link'),
227 dialog = Dialog.create({
228 title: gettext('Edit link'),
229 executeButtonText: gettext('Apply'),
230 cancelButtonText: gettext('Cancel'),
232 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
237 dialog.on('execute', function(event) {
238 doc.transaction(function() {
239 link.setAttr('href', event.formData.href);
243 description: action.getState().description
253 fragment: {type: 'context', name: 'fragment'}
256 label: gettext('link')
258 getState: function(params) {
259 if(!params.fragment || !params.fragment.isValid()) {
260 return {allowed: false};
263 if(params.fragment instanceof params.fragment.TextRangeFragment) {
264 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
265 return {allowed: false};
269 description: gettext('Create link from selection'),
270 execute: createLinkFromSelection
274 if(params.fragment instanceof params.fragment.CaretFragment) {
275 if(params.fragment.node.isInside('link')) {
276 return {allowed: true, toggled: true, execute: editLink};
279 return {allowed: false};
285 undoRedoAction('undo'),
286 undoRedoAction('redo'),
288 createWrapTextAction({name: 'emphasis', klass: 'emp', description: gettext('Mark as emphasized')}),
289 createWrapTextAction({name: 'cite', klass: 'cite', description: gettext('Mark as citation')}),
291 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
295 plugin.config = function(config) {
296 // templates.actions[0].config(config.templates);
297 templates.actions[0].params.template.options = config.templates;