forked from adamsanderson/caret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
127 lines (108 loc) · 3.73 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Caret</title>
<meta name="author" content="Adam Sanderson">
<link rel="stylesheet" href="build/build.css" type="text/css" media="screen" charset="utf-8">
<style type="text/css" media="screen">
body {
padding: 20px;
font-family: sans-serif;
background: white;
}
footer {
position: fixed;
bottom: 20px;
background: white;
}
.parent-element {
outline: 1px solid #F00;
}
span.out {
color: #666;
background: #eee;
}
.caret {
color: red;
}
</style>
</head>
<body>
<h1>Caret</h1>
<div id='info'>
<p>
Before | After Caret:
<span class='out' id='text-before'></span><span class='caret'>|</span><span class='out' id='text-after'></span>
</p>
<p>
Current Tag: <span class='out' id='current-tag'> </span>
</p>
</div>
<div>
Move the caret
<a href='#' id='move-to-start'>to start</a> |
<a href='#' id='move-to-end'>to end</a> |
<a href='#' id='move-before'>before "code" element</a> |
<a href='#' id='move-after'>after "code" element</a>
</div>
<div contenteditable="true" id='content'>
<p>
This is a <i>demo</i> of the <code id='code-el'>Caret</code> component.
Click here and move the caret around either with the mouse or the keyboard.
</p>
<p>
You can use this to:
</p>
<ul>
<li>Simplify autocompletion</li>
<li>Build <a href='http://adamsanderson.github.com/wysiwyg/'>WYSIWYG</a> tools</li>
<li>Other great things!</li>
</ul>
</div>
<footer>
See the <a href='https://github.com/adamsanderson/caret/'>GitHub repository</a> for more information.
</footer>
<script src="build/build.js" type="text/javascript" charset="utf-8"></script>
<script>
var Caret = require('caret');
var events = require('component-event');
var el = document.getElementById('content');
var before = document.getElementById('text-before');
var after = document.getElementById('text-after');
var tag = document.getElementById('current-tag');
var caret = new Caret(el);
caret.on('change', function(){
var lastParent = document.querySelector('.parent-element');
var currentParent = this.parentElement();
if (lastParent != currentParent){
tag.textContent = tag.innerText = currentParent.tagName;
}
before.textContent = before.innerText = caret.textBefore();
after.textContent = after.innerText = caret.textAfter();
});
events.bind(document.getElementById('move-to-start'), 'click', function(){
caret.moveToStart();
});
events.bind(document.getElementById('move-to-end'), 'click', function(){
caret.moveToEnd();
});
events.bind(document.getElementById('move-before'), 'click', function(){
caret.moveBefore(getRelativeDemoItem());
});
events.bind(document.getElementById('move-after'), 'click', function(){
caret.moveAfter(getRelativeDemoItem());
});
// For the moment, pick a relative item to demo against:
function getRelativeDemoItem(){
var el = document.getElementsByTagName('code')[0];
if (!el) {
el = document.createElement('code');
el.textContent = el.innerText = "Hey you deleted the code element, here's a new one."
content.appendChild(el);
}
return el;
}
</script>
</body>
</html>