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 desc += ': ' + (_.last(params.document[dir+'Stack']).metadata || gettext('unknown operation'));
70 description: allowed ? desc : descEmpty
76 var pad = function(number) {
78 number = '0' + number;
86 fragment: {type: 'context', name: 'fragment'}
90 execute: function(params, editor) {
92 var node = params.fragment.node;
93 if(node.nodeType === Node.TEXT_NODE) {
96 node.document.transaction(function() {
97 var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}});
98 comment.append({text:''});
99 var user = editor.getUser(), creator;
103 creator += ' (' + user.email + ')';
106 creator = 'anonymous';
109 var currentDate = new Date(),
110 dt = pad(currentDate.getDate()) + '-' +
111 pad((currentDate.getMonth() + 1)) + '-' +
112 pad(currentDate.getFullYear()) + ' ' +
113 pad(currentDate.getHours()) + ':' +
114 pad(currentDate.getMinutes()) + ':' +
115 pad(currentDate.getSeconds());
117 var metadata = comment.getMetadata();
118 metadata.add({key: 'creator', value: creator});
119 metadata.add({key: 'date', value: dt});
123 getState: function(params) {
125 allowed: params.fragment && params.fragment.isValid() &&
126 params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
129 state.description = gettext('Insert comment after current node');
136 var createWrapTextAction = function(createParams) {
138 name: createParams.name,
140 fragment: {type: 'context', name: 'fragment'},
142 getState: function(params) {
144 label: this.config.label
149 !params.fragment || !params.fragment.isValid() ||
150 !(params.fragment instanceof params.fragment.TextRangeFragment) ||
151 !params.fragment.hasSiblingBoundries()) {
152 return _.extend(state, {allowed: false});
155 parent = params.fragment.startNode.parent();
156 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
157 return _.extend(state, {allowed: false});
160 return _.extend(state, {
162 execute: function(params) {
163 params.fragment.document.transaction(function() {
164 var parent = params.fragment.startNode.parent();
165 return parent.wrapText({
166 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
167 offsetStart: params.fragment.startOffset,
168 offsetEnd: params.fragment.endOffset,
169 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
179 var createLinkFromSelection = function(params) {
180 var doc = params.fragment.document,
181 dialog = Dialog.create({
182 title: gettext('Create link'),
183 executeButtonText: gettext('Apply'),
184 cancelButtonText: gettext('Cancel'),
186 {label: gettext('Link'), name: 'href', type: 'input'}
190 dialog.on('execute', function(event) {
191 doc.transaction(function() {
192 var span = params.fragment.startNode.parent().wrapText({
193 _with: {tagName: 'span', attrs: {'class': 'link'}},
194 offsetStart: params.fragment.startOffset,
195 offsetEnd: params.fragment.endOffset,
196 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
198 span.setAttr('href', event.formData.href);
206 var editLink = function(params) {
207 var doc = params.fragment.document,
208 link = params.fragment.node.getParent('link'),
209 dialog = Dialog.create({
210 title: gettext('Edit link'),
211 executeButtonText: gettext('Apply'),
212 cancelButtonText: gettext('Cancel'),
214 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
218 dialog.on('execute', function(event) {
219 doc.transaction(function() {
220 link.setAttr('href', event.formData.href);
230 fragment: {type: 'context', name: 'fragment'}
233 label: gettext('link')
235 getState: function(params) {
236 if(!params.fragment || !params.fragment.isValid()) {
237 return {allowed: false};
240 if(params.fragment instanceof params.fragment.TextRangeFragment) {
241 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
242 return {allowed: false};
246 description: gettext('Create link from selection'),
247 execute: createLinkFromSelection
251 if(params.fragment instanceof params.fragment.CaretFragment) {
252 if(params.fragment.node.isInside('link')) {
253 return {allowed: true, toggled: true, execute: editLink};
256 return {allowed: false};
262 undoRedoAction('undo'),
263 undoRedoAction('redo'),
265 createWrapTextAction({name: 'emphasis', klass: 'emp'}),
266 createWrapTextAction({name: 'cite', klass: 'cite'}),
268 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
272 plugin.config = function(config) {
273 // templates.actions[0].config(config.templates);
274 templates.actions[0].params.template.options = config.templates;