Commit e480510c authored by Michael Murtaugh's avatar Michael Murtaugh

refactoring is fun

parent b20954d6
......@@ -11,22 +11,68 @@
</style>
</head>
<body>
<a id="use" href="test.etherpad.json">test</a>
<a href="constantcodeofconduct2.etherpad.json">constantcodeofconduct2</a>
<div>
<h2>sources:</h2>
<ul id="sources">
<li><a id="use" href="test.etherpad.json">test</a></li>
<li><a href="constantcodeofconduct2.etherpad.json">constantcodeofconduct2</a></li>
</ul>
<span id="info"><span id="rev">&mdash;</span> / <span id="total_revs">&mdash;</span></span>
<button id="prev">&larr;</button>
<button id="next">&rarr;</button>
<input type="checkbox" id="animate"><label for="animate">animate</label>
</div>
<div id="content"></div>
<script src="dist/etherpad.js"></script>
<script>
async function init () {
var link = document.getElementById("use"),
pad = await etherpad.Etherpad.load(link.href, link.textContent),
div = document.createElement("div");
console.log("pad", pad);
document.body.appendChild(div);
pad.animate(change => {
div.innerHTML = change.curline;
}, 100);
let content = document.getElementById("content"),
div = document.createElement("div"),
rev = document.getElementById("rev"),
total_revs = document.getElementById("total_revs"),
prev = document.getElementById("prev"),
next = document.getElementById("next"),
current_rev = 0,
pad;
content.appendChild(div);
function show_changeset (n) {
rev.innerHTML = n;
content.textContent = pad.changesets[n].raw;
}
document.addEventListener("DOMContentLoaded", init);
next.addEventListener("click", e => {
if (current_rev < pad.last_rev) { show_changeset(++current_rev); }
})
prev.addEventListener("click", e => {
if (current_rev > 0) { show_changeset(--current_rev); }
})
async function load_link (link) {
pad = await etherpad.Etherpad.load(link.href, link.textContent);
total_revs.innerHTML = pad.last_rev;
current_rev = 0;
show_changeset(current_rev);
// console.log("pad", pad);
// pad.animate(change => {
// div.innerHTML = change.curline;
// }, 100);
}
var sources = document.querySelectorAll("#sources a");
for (let i=0, l=sources.length; i<l; i++) {
let source = sources[i];
source.addEventListener("click", e => {
e.preventDefault();
load_link(source);
})
}
document.addEventListener("DOMContentLoaded", e => {
load_link(document.getElementById("use"));
});
</script>
</body>
......
......@@ -33,6 +33,7 @@ var etherpad = (function (exports) {
ops_raw = m[4],
op = null,
ret = {
raw: c,
source_length: parseInt(m[1], 36),
final_op: m[2],
final_diff: parseInt(m[3], 36),
......@@ -47,14 +48,16 @@ var etherpad = (function (exports) {
return ret;
}
async function wait (time) {
return new Promise(resolve => {
setTimeout(resolve, time);
})
}
class Etherpad {
constructor (padname) {
this.padname = padname;
this.text = "\n";
this.changesets = null;
}
static async load (filename, padname) {
console.log("loading json...");
// console.log("loading json...");
let resp = await fetch(filename),
data = await resp.json(),
pad_data = data[`pad:${padname}`];
......@@ -62,6 +65,13 @@ var etherpad = (function (exports) {
pad.parse_json(data);
return pad;
}
constructor (padname) {
this.padname = padname;
this.text = "\n";
this.changesets = null;
}
async animate (callback, steptime) {
var curtext = "\n";
for (var i=0; i<=this.last_rev; i++) {
......@@ -73,6 +83,7 @@ var etherpad = (function (exports) {
}
//console.log("CHECK", (curtext == pad_data.atext.text))
}
parse_json (data) {
let pad_data = data[`pad:${this.padname}`],
last_rev = pad_data.head;
......@@ -89,6 +100,7 @@ var etherpad = (function (exports) {
// console.log("done", last_rev)
return last_rev;
}
perform_changeset (input_text, c) {
let textpos = 0,
bank = c.bank,
......@@ -117,6 +129,7 @@ var etherpad = (function (exports) {
newtext += input_text.substring(textpos);
return newtext;
}
perform_changeset_curline (input_text, c) {
let textpos = 0,
bank = c.bank,
......@@ -158,11 +171,6 @@ var etherpad = (function (exports) {
return {text: newtext, curline: curline};
}
}
async function wait (time) {
return new Promise(resolve => {
setTimeout(resolve, time);
})
}
exports.Etherpad = Etherpad;
......
......@@ -30,6 +30,7 @@ function changeset_parse (c) {
ops_raw = m[4],
op = null,
ret = {
raw: c,
source_length: parseInt(m[1], 36),
final_op: m[2],
final_diff: parseInt(m[3], 36),
......@@ -44,14 +45,16 @@ function changeset_parse (c) {
return ret;
}
async function wait (time) {
return new Promise(resolve => {
setTimeout(resolve, time);
})
}
export class Etherpad {
constructor (padname) {
this.padname = padname;
this.text = "\n";
this.changesets = null;
}
static async load (filename, padname) {
console.log("loading json...");
// console.log("loading json...");
let resp = await fetch(filename),
data = await resp.json(),
pad_data = data[`pad:${padname}`];
......@@ -59,6 +62,13 @@ export class Etherpad {
pad.parse_json(data);
return pad;
}
constructor (padname) {
this.padname = padname;
this.text = "\n";
this.changesets = null;
}
async animate (callback, steptime) {
var curtext = "\n";
for (var i=0; i<=this.last_rev; i++) {
......@@ -70,6 +80,7 @@ export class Etherpad {
}
//console.log("CHECK", (curtext == pad_data.atext.text))
}
parse_json (data) {
let pad_data = data[`pad:${this.padname}`],
last_rev = pad_data.head;
......@@ -86,6 +97,7 @@ export class Etherpad {
// console.log("done", last_rev)
return last_rev;
}
perform_changeset (input_text, c) {
let textpos = 0,
textline = 0,
......@@ -117,6 +129,7 @@ export class Etherpad {
newtext += input_text.substring(textpos);
return newtext;
}
perform_changeset_curline (input_text, c) {
let textpos = 0,
textline = 0,
......@@ -161,8 +174,4 @@ export class Etherpad {
return {text: newtext, curline: curline};
}
}
async function wait (time) {
return new Promise(resolve => {
setTimeout(resolve, time);
})
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment