Files
Mergely/examples/ajax.html
2021-12-29 11:50:13 +00:00

132 lines
3.9 KiB
HTML

<!--
This example demonstrates how to set left and right editors using ajax.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" /><title>Mergely - Simple Example</title>
<meta http-equiv="X-UA-Compatible" content="chrome=1, IE=edge">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta name="description" content="Merge and Diff your documents with diff online and share" />
<meta name="keywords" content="mergely,diff,merge,compare" />
<meta name="author" content="Jamie Peabody" />
<!-- CodeMirror peer dependency -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.32.0/codemirror.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.32.0/addon/search/searchcursor.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.32.0/codemirror.min.css" />
<!-- Mergely -->
<script type="text/javascript" src="/lib/mergely.js"></script>
<link type="text/css" rel="stylesheet" href="/lib/mergely.css" />
<style type='text/css'>
.drop-zone {
border: 2px dashed #BBB;
border-radius: 5px;
color: #BBB;
padding: 10px 25px;
text-align: center;
align: center;
width: 80%;
}
.drop-zone.hover {
border-color: #000;
}
label:hover { cursor: pointer; }
#summary { color: #aaa; }
</style>
<script type="text/javascript">
document.onreadystatechange = function () {
if (document.readyState !== 'complete') {
return;
}
const doc = new Mergely('#mergely', {
license: 'lgpl',
ignorews: false,
wrap_lines: false,
change_timout: 0
});
// toggle options listners
document.getElementById('ignorews').addEventListener('change', (ev) => {
doc.options({ ignorews: ev.target.checked });
}, false);
document.getElementById('wraplines').addEventListener('change', (ev) => {
doc.options({ wrap_lines: ev.target.checked });
}, false);
// summary listner
doc.on('updated', function () {
const changes = doc.summary();
console.log(changes);
const summary = [];
const types = { a: 'added', c: 'changed', d: 'deleted' };
for (const t in types) {
const type = types[t];
if (!changes[t]) {
continue;
}
summary.push(`${changes[t]} ${type}`);
}
if (!summary.length) {
summary.push(`no changes`);
}
document.getElementById('summary').innerHTML = summary.join(', ');
});
// download listener
function setDownloadContent(target, side) {
const value = doc.get(side);
const dataUri = `data:plain/text;charset=UTF-8,${encodeURIComponent(value)}`;
target.setAttribute('download', `${side}.txt`);
target.setAttribute('href', dataUri);
}
document.getElementById('save-lhs').addEventListener('mouseover', (ev) => {
setDownloadContent(ev.target, 'lhs');
});
document.getElementById('save-rhs').addEventListener('mouseover', (ev) => {
setDownloadContent(ev.target, 'rhs');
});
// async fetch
fetch('/examples/lhs.txt')
.then((resp) => resp.text())
.then(data => doc.lhs(data));
fetch('/examples/rhs.txt')
.then((resp) => resp.text())
.then(data => doc.rhs(data));
};
</script>
</head>
<body style="width: 100%; margin: 0;">
<div style="display:flex">
<span>
<input type="checkbox" id="ignorews"><label for="ignorews">ignore white-space</label>
</span>
<span>
<input type="checkbox" id="wraplines"><label for="wraplines">wrap lines</label>
</span>
</div>
<div style="display:flex">
<span style="flex-grow:1;">
<a id="save-lhs" class="save-link" href="#">save</a>
</span>
<span style="flex-grow:1;margin-left:28px;">
<a id="save-rhs" class="save-link" href="#">save</a>
</span>
</div>
<div style="height: 450px; width: 100%;">
<div class="mergely-resizer">
<div id="mergely">
</div>
</div>
</div>
<div id="summary" />
</body>
</html>