-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (122 loc) · 4.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var keys = require('keys')
, query = require('query')
, angular = require('angularjs')
, events = require('event')
// angular modules
, tags = require('tags')
, contentEditable = require('contenteditable');
var settings = require('settings')('note')
, angularSettings = require('angular-settings');
var settingsShortcut = require('settings-shortcut');
angularSettings.register('keyboard-shortcut', settingsShortcut);
var template = require('./template')
, defaultSettings = require('./settings')
, makeKeyMap = require('./keymap').makeKeyMap
, Events = require('scoped-events')
, makeMovers = require('./movement').makeMovers;
settings.config(defaultSettings);
function fragmentEmpty(fragment) {
var d = document.createElement('div');
d.appendChild(fragment);
return d.innerText.length === 0;
}
module.exports = angular.module('note', [tags.name, contentEditable.name])
.directive('note', ['$compile', 'events',
function ($compile, io) {
return {
scope: {},
replace: true,
restrict: 'A',
link: function (scope, element, attrs) {
var name = attrs.note;
element.html(template);
$compile(element.contents())(scope);
var title;
// bind note
scope.$parent.$watch(name, function(value) {
scope.note = value;
title = value.title;
});
scope.$watch('note', function(value) {
scope.$parent[name] = value;
title = value.title;
});
// lookup other things
scope.parent = null;
if (scope.$parent.$parent &&
scope.$parent.$parent.move) {
scope.parent = scope.$parent.$parent;
}
scope.el = element[0];
scope.title = query('.title', scope.el);
scope.body = query('.body', scope.el);
scope.index = function () {
return scope.$parent.$index;
};
scope.move = makeMovers(io, scope);
scope.keydown = makeKeyMap(settings, scope);
var keyhash = settings.getHashKeys(scope.keydown);
keyhash.left = function (e) {
var range = window.getSelection().getRangeAt(0)
, clone = range.cloneRange();
if (!range.collapsed) return true;
clone.selectNodeContents(scope.title);
clone.setEnd(range.endContainer, range.endOffset);
if (clone.toString().trim().length !== 0) return true;
scope.keydown['nav.goUp'](e, true);
};
keyhash.right = function (e) {
var range = window.getSelection().getRangeAt(0)
, clone = range.cloneRange();
if (!range.collapsed) return true;
clone.selectNodeContents(scope.title);
clone.setStart(range.endContainer, range.endOffset);
if (clone.toString().trim().length !== 0) return true;
scope.keydown['nav.goDown'](e);
};
keyhash.backspace = function (e) {
if (!scope.parent) return;
if (scope.note.title.trim().length > 0 && scope.note.title !== '<br>') {
var range = window.getSelection().getRangeAt(0)
, clone = range.cloneRange();
if (!range.collapsed) return true;
clone.selectNodeContents(scope.title);
clone.setEnd(range.endContainer, range.endOffset);
if (!fragmentEmpty(clone.cloneContents())) return true;
} else {
scope.note.title = '';
}
scope.deleted = true;
scope.parent.move.removeChild(scope);
};
var keydown = keys(keyhash);
events.bind(scope.title, 'keydown', keydown);
var dirtyTimeout = null
, longerTimeout = null;
function saveTitle() {
if (dirtyTimeout) clearTimeout(dirtyTimeout);
if (longerTimeout) clearTimeout(longerTimeout);
dirtyTimeout = null;
longerTimeout = null;
if (scope.deleted) return;
if (scope.note.title === title) return;
title = scope.note.title;
io.emit('change', {
path: [],
id: scope.note.properties.id
});
console.log('saving');
}
events.bind(scope.title, 'keydown', function () {
if (dirtyTimeout) clearTimeout(dirtyTimeout);
dirtyTimeout = setTimeout(saveTitle, 500);
if (!longerTimeout) setTimeout(saveTitle, 2000);
});
events.bind(scope.title, 'blur', saveTitle);
}
};
}])
.factory('events', function () {
var Emitter = require('emitter');
return new Emitter();
});