- var canvas = this;
- this.wrapper.on('keydown', function(e) {
- if(e.which === 13) {
- e.preventDefault();
- var cursor = canvas.getCursor();
- if(!cursor.isSelecting()) {
- var position = cursor.getPosition(),
- elements = position.element.split({offset: position.offset}),
- newEmpty,
- goto;
-
- if(position.offsetAtBeginning)
- newEmpty = elements.first;
- else if(position.offsetAtEnd)
- newEmpty = elements.second;
- if(newEmpty) {
- goto = newEmpty.append(documentElement.DocumentTextElement.create({text: ''}, this));
- canvas.setCurrentElement(goto);
- }
- }
- }
- });
-
-
- var KEYS = {
- ARROW_LEFT: 37,
- ARROW_UP: 38,
- ARROW_RIGHT: 39,
- ARROW_DOWN: 40
- }
-
- this.wrapper.on('keyup', function(e) {
- if(e.which >= 37 && e.which <= 40) {
- var element = canvas.getCursor().getPosition().element,
- caretTo = false;
- if(!element) {
- // Chrome hack
- var direction;
- if(e.which === KEYS.ARROW_LEFT || e.which === KEYS.ARROW_UP) {
- direction = 'above';
- caretTo = 'end';
- } else {
- direction = 'below';
- caretTo = 'start';
- }
- element = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]:visible', direction, window.getSelection().focusNode));
- }
- canvas.setCurrentElement(element, {caretTo: caretTo});
- }
- });
-
- this.wrapper.on('keydown', function(e) {
- if(e.which >= 37 && e.which <= 40) {
- var position = canvas.getCursor().getPosition(),
- element = position.element;
- if(element && (element instanceof documentElement.DocumentTextElement)) {
- if(element.isEmpty()) {
- var direction, caretTo;
- if(e.which === KEYS.ARROW_LEFT || e.which === KEYS.ARROW_UP) {
- direction = 'above';
- caretTo = 'end';
- } else {
- direction = 'below';
- caretTo = 'start';
- }
- var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, window.getSelection().focusNode))
- canvas.setCurrentElement(element, {caretTo: caretTo});
- } else {
- var txt = element.dom().contents()[0].data;
- if(e.which === KEYS.ARROW_LEFT && position.offset > 1 && txt.charAt(position.offset-2) === utils.unicode.ZWS) {
- e.preventDefault();
- canvas._moveCaretToTextElement(element, position.offset-2);
- }
- if(e.which === KEYS.ARROW_RIGHT && position.offset < txt.length - 1 && txt.charAt(position.offset+1) === utils.unicode.ZWS) {
- e.preventDefault();
- canvas._moveCaretToTextElement(element, position.offset+2);
- }
- }
- }
-
-
- }
- });