smartxml: testing three available levels of transformation undo-awareness
[fnpeditor.git] / src / editor / plugins / core.js
1 define([
2
3 ], function() {
4     
5 'use strict';
6
7 var breakContentTransformation = {
8     impl: function(args) {
9         var node = this.context,
10             newNodes, emptyNode, emptyText;
11         newNodes = node.transform('smartxml.split', {offset: args.offset});
12         [newNodes.first, newNodes.second].some(function(newNode) {
13             if(!(newNode.contents().length)) {
14                 newNode.transform('smartxml.append', {text: ''});
15                 return true; // break
16             }
17         });
18         return _.extend(newNodes, {emptyText: emptyText});
19     },
20     isAllowed: function() {
21
22     }
23 };
24
25
26 var breakContentAction = function(document, context) { //@ editor.getDocument(); editor.getContext('...')
27     var textNode = context.cursor.currentNode;
28     if(textNode) {
29         var result, goto;
30
31         result = textNode.transform('core.break-content', {offset: context.cursor.offset});
32
33         if(result.emptyText) {
34             goto = result.createdEmpty;
35             gotoOptions = {};
36         } else {
37             goto = result.second;
38             gotoOptions = {caretTo: 'start'};   
39         }
40
41         context.setCurrentElement(goto, gotoOptions);
42     }
43 }
44 breakContentAction.isAllowed = function(document, context) {
45     /* globals Node */
46     var node = context.cursor.currentNode;
47     return node.nodeType === Node.TEXT_NODE;
48 }
49
50 return {
51     keyHandlers: [
52         {key: 'ENTER', target: 'main-document-area', handler: function(editor) {
53             var action = editor.getAction('core.break-document-content');
54             if(action.isAllowed()) {
55                 action.execute();
56             }
57         }},
58         {key: 'ENTER', target: 'main-document-area', actionHandler: 'core.break-document-content'}
59     ],
60     
61     documentActions: [
62         {
63             name: 'core.break-document-content',
64             context: 'main-document-area',
65             label: 'break here'
66             icon: 'core:some-name',
67             action: breakContentAction
68         }
69     ],
70
71     // zapisywanie dokumentu:
72
73     documentTransformations: [
74         {name: 'core.break-content', textNode: true, t: breakContentTransformation},
75
76         // transformacja z poziomu smartxml
77         {name: 'core.wrap-with', textNode: true, t: wrapWith}
78
79         // list plugin:
80         {name: 'list.remove-list', elementNode: 'list', t: null}
81         // hipotetyczna akcja na itemie listy
82         {name: 'list.extract', elementNode: 'item', requiresParent: 'list', requiresInParents: '?'}
83     ]
84 };
85
86 });