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');
67 description: allowed ? desc : descEmpty
73 var pad = function(number) {
75 number = '0' + number;
83 fragment: {type: 'context', name: 'fragment'}
87 execute: function(params, editor) {
89 var node = params.fragment.node;
90 if(node.nodeType === Node.TEXT_NODE) {
93 node.document.transaction(function() {
94 var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}});
95 comment.append({text:''});
96 var user = editor.getUser(), creator;
100 creator += ' (' + user.email + ')';
103 creator = 'anonymous';
106 var currentDate = new Date(),
107 dt = pad(currentDate.getDate()) + '-' +
108 pad((currentDate.getMonth() + 1)) + '-' +
109 pad(currentDate.getFullYear()) + ' ' +
110 pad(currentDate.getHours()) + ':' +
111 pad(currentDate.getMinutes()) + ':' +
112 pad(currentDate.getSeconds());
114 var metadata = comment.getMetadata();
115 metadata.add({key: 'creator', value: creator});
116 metadata.add({key: 'date', value: dt});
120 getState: function(params) {
122 allowed: params.fragment && params.fragment.isValid() &&
123 params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
126 state.description = gettext('Insert comment after current node');
133 var createWrapTextAction = function(createParams) {
135 name: createParams.name,
137 fragment: {type: 'context', name: 'fragment'},
139 getState: function(params) {
141 label: this.config.label
146 !params.fragment || !params.fragment.isValid() ||
147 !(params.fragment instanceof params.fragment.TextRangeFragment) ||
148 !params.fragment.hasSiblingBoundries()) {
149 return _.extend(state, {allowed: false});
152 parent = params.fragment.startNode.parent();
153 if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
154 return _.extend(state, {allowed: false});
157 return _.extend(state, {
159 execute: function(params) {
160 params.fragment.document.transaction(function() {
161 var parent = params.fragment.startNode.parent();
162 return parent.wrapText({
163 _with: {tagName: 'span', attrs: {'class': createParams.klass}},
164 offsetStart: params.fragment.startOffset,
165 offsetEnd: params.fragment.endOffset,
166 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
176 var createLinkFromSelection = function(params) {
177 var doc = params.fragment.document,
178 dialog = Dialog.create({
179 title: gettext('Create link'),
180 executeButtonText: gettext('Apply'),
181 cancelButtonText: gettext('Cancel'),
183 {label: gettext('Link'), name: 'href', type: 'input'}
187 dialog.on('execute', function(event) {
188 doc.transaction(function() {
189 var span = params.fragment.startNode.parent().wrapText({
190 _with: {tagName: 'span', attrs: {'class': 'link'}},
191 offsetStart: params.fragment.startOffset,
192 offsetEnd: params.fragment.endOffset,
193 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
195 span.setAttr('href', event.formData.href);
203 var editLink = function(params) {
204 var doc = params.fragment.document,
205 link = params.fragment.node.getParent('link'),
206 dialog = Dialog.create({
207 title: gettext('Edit link'),
208 executeButtonText: gettext('Apply'),
209 cancelButtonText: gettext('Cancel'),
211 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
215 dialog.on('execute', function(event) {
216 doc.transaction(function() {
217 link.setAttr('href', event.formData.href);
227 fragment: {type: 'context', name: 'fragment'}
230 label: gettext('link')
232 getState: function(params) {
233 if(!params.fragment || !params.fragment.isValid()) {
234 return {allowed: false};
237 if(params.fragment instanceof params.fragment.TextRangeFragment) {
238 if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
239 return {allowed: false};
243 description: gettext('Create link from selection'),
244 execute: createLinkFromSelection
248 if(params.fragment instanceof params.fragment.CaretFragment) {
249 if(params.fragment.node.isInside('link')) {
250 return {allowed: true, toggled: true, execute: editLink};
253 return {allowed: false};
259 undoRedoAction('undo'),
260 undoRedoAction('redo'),
262 createWrapTextAction({name: 'emphasis', klass: 'emp'}),
263 createWrapTextAction({name: 'cite', klass: 'cite'}),
265 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
269 plugin.config = function(config) {
270 // templates.actions[0].config(config.templates);
271 templates.actions[0].params.template.options = config.templates;