convert-rich-text
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -6,5 +6,5 @@ var Doc = require('./lib/doc'); | ||
for (var i = 0; i < delta.ops.length; i++) { | ||
doc.write(delta.ops[i]); | ||
doc.writeOp(delta.ops[i]); | ||
} | ||
return doc.getHTML(); | ||
}; |
@@ -10,6 +10,9 @@ var dom = require('./dom'); | ||
this.root = document.createElement('div'); | ||
this.newLine(); | ||
} | ||
Doc.prototype.write = function(op) { | ||
Doc.prototype.getHTML = function() { | ||
return this.root.innerHTML; | ||
}; | ||
Doc.prototype.writeOp = function(op) { | ||
if (op.insert === null || op.insert === undefined) { | ||
@@ -22,15 +25,13 @@ throw new Error('Cannot convert delta with non-insert operations'); | ||
op.insert.replace(/\r\n?/g, '\n') : '!'; | ||
var lineTexts = text.split('\n'); | ||
var index = text.indexOf('\n'); | ||
// TODO: each loop iteration may be async | ||
lineTexts.forEach(function(lineText, i) { | ||
if (lineText.length) { | ||
this.appendToLine(lineText, attrs); | ||
} | ||
while (index >= 0) { | ||
this.writeText(text.slice(0, index), attrs); | ||
this.formatLine(attrs); | ||
this.line = null; | ||
text = text.slice(index + 1); | ||
index = text.indexOf('\n'); | ||
} | ||
if (i < lineTexts.length - 1) { | ||
this.formatLine(attrs); | ||
this.newLine(); | ||
} | ||
}.bind(this)); | ||
this.writeText(text, attrs); | ||
@@ -40,16 +41,12 @@ return this; | ||
Doc.prototype.getHTML = function() { | ||
// HACK: fix this | ||
return this.root.innerHTML.replace(/<([a-z\-]+)><\/\1>$/, ''); | ||
}; | ||
Doc.prototype.writeText = function(text, attrs) { | ||
if (!text.length) { return } | ||
Doc.prototype.newLine = function() { | ||
var node = document.createElement(this.blockTag); | ||
this.root.appendChild(node); | ||
this.line = node; | ||
}; | ||
Doc.prototype.appendToLine = function(text, attrs) { | ||
var node = document.createTextNode(text); | ||
if (!this.line) { | ||
this.line = document.createElement(this.blockTag); | ||
this.root.appendChild(this.line); | ||
} | ||
this.line.appendChild(node); | ||
@@ -59,8 +56,9 @@ | ||
// TODO: each format function returns a promise | ||
Object.keys(attrs).forEach(function(name) { | ||
var format = this.formats[name]; | ||
for (var key in attrs) { | ||
if (!attrs.hasOwnProperty(key)) { continue } | ||
var format = this.formats[key]; | ||
if (format && format.type !== 'line') { | ||
node = this.applyFormat(node, format, attrs[name]); | ||
node = this.applyFormat(node, format, attrs[key]); | ||
} | ||
}.bind(this)); | ||
} | ||
@@ -121,3 +119,4 @@ // TODO: optimize line? | ||
// TODO: each format function returns a promise | ||
Object.keys(attrs).forEach(function(name) { | ||
for (var name in attrs) { | ||
if (!attrs.hasOwnProperty(name)) { continue } | ||
var format = this.formats[name]; | ||
@@ -127,5 +126,5 @@ if (format && format.type === 'line') { | ||
} | ||
}.bind(this)); | ||
} | ||
this.line = line; | ||
}; |
{ | ||
"name": "convert-rich-text", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Convert an insert-only rich-text delta into HTML", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -40,6 +40,15 @@ var assert = require('chai').assert; | ||
{ | ||
desc: 'No formats', | ||
delta: { ops: [ | ||
{insert: 'Hello world\n'} | ||
]}, | ||
expected: | ||
'<div>Hello world</div>' | ||
}, | ||
{ | ||
desc: 'Simple inline tags', | ||
delta: { ops: [ | ||
{insert: 'Hello, '}, | ||
{insert: 'World!\n', attributes: {bold: true}} | ||
{insert: 'World!', attributes: {bold: true}}, | ||
{insert: '\n'} | ||
]}, | ||
@@ -59,3 +68,4 @@ expected: | ||
{insert: ' '}, | ||
{insert: 'Google', attributes: {link: 'https://www.google.com'}} | ||
{insert: 'Google', attributes: {link: 'https://www.google.com'}}, | ||
{insert: '\n'} | ||
]}, | ||
@@ -73,2 +83,3 @@ expected: | ||
{insert: 'Hello world', attributes: { color: 'red', user: 1234 }}, | ||
{insert: '\n'} | ||
]}, | ||
@@ -75,0 +86,0 @@ expected: |
25715
440