Skip to content

Instantly share code, notes, and snippets.

@foo9
Last active December 23, 2015 05:59
Show Gist options
  • Select an option

  • Save foo9/6590945 to your computer and use it in GitHub Desktop.

Select an option

Save foo9/6590945 to your computer and use it in GitHub Desktop.
contenteditableを使って簡単にドキュメントを翻訳できる仕組みを作りたい。
(function() {
var map = {};
var elems = document.querySelectorAll('h1, h2, h3, h4, h5, h6, h7, p, li, dt, dd');
var cid;
var text;
for (var i = 0, iz = elems.length; i < iz; i++) {
cid = 'c' + i;
elems[i].setAttribute('contenteditable', 'true');
elems[i].setAttribute('data-cid', cid);
text = elems[i].textContent;
map[cid] = { 'original': text, 'renew': text };
elems[i].addEventListener('keyup', function() {
map[this.getAttribute('data-cid')]['renew'] = '' + this.textContent;
}, false);
}
var TextMap = (function() {
function TextMap(map) {
this.map = map;
}
TextMap.prototype.toCSV = function() {
var csv = [];
var index = 1;
csv[0] = ['"cid"', '"original"', '"renew"'].join(',');
for (var cid in this.map) {
csv[index] = [
'"' + cid + '"',
'"' + (this.map[cid]['original']).replace(/"/g, '""') + '"',
'"' + (this.map[cid]['renew']).replace(/"/g, '""') + '"'
].join(',');
index = index + 1;
}
return csv.join('\n');
};
return TextMap;
})();
this.textMap = new TextMap(map);
}).call(this);
console.log(textMap.toCSV());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment