Буквы должны быть интервалами, а слова - строчными блоками.
Я даже добавил поддержку абзацев.
const text = `
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
`
const main = () => {
render(text, '.target')
}
const render = (text, target) => {
if (typeof target === 'string') {
target = document.querySelector(target)
}
removeChildren(target)
text.trim().split(/\n/g).forEach((paragraph, pIndex, pArr) => {
let paragraphEl = document.createElement('div')
paragraphEl.classList.add('paragraph')
paragraph.split(/\s+/g).forEach((word, wIndex, wArr) => {
let wordEl = document.createElement('div')
wordEl.classList.add('word')
word.split('').forEach((letter, lIndex, lArr) => {
let letterEl = document.createElement('span')
letterEl.classList.add('letter')
letterEl.textContent = letter
wordEl.appendChild(letterEl)
})
paragraphEl.appendChild(wordEl)
if (wIndex < wArr.length) {
let spaceEl = document.createElement('span')
spaceEl.classList.add('space')
spaceEl.innerHTML = ' '
paragraphEl.appendChild(spaceEl)
}
})
target.appendChild(paragraphEl)
})
}
const removeChildren = (el) => {
while (el.firstChild) {
el.firstChild.remove()
}
}
main()
.target {
width: 20em;
overflow: hidden;
border: thin solid grey;
}
.paragraph {
margin: 0.33em 0.5em;
}
.word {
display: inline-block;
}
.word:hover {
background: #FFA;
}
<div class="target"></div>
Упрощенный пример дерева DOM
Это просто интересный способ продемонстрировать процесс создания элементов.
const text = `
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
`
const main = () => {
render(text, '.page')
}
const render = (text, target) => {
appendChildren(target, text.trim().split(/\n/g).map(
(paragraph, pIndex, pArr) => {
return {
tag: 'div',
options: {
class: 'paragraph',
data: { paragraphIndex : pIndex },
children: paragraph.split(/\s+/g).reduce(
(children, word, wIndex, wArr) => {
children.push({
tag: 'div',
options: {
class: 'word',
children: word.split('').map((letter) => {
return {
tag: 'span',
options: {
class: 'letter',
text: letter
}
}
})
}
})
if (wIndex < wArr.length) {
children.push({
tag: 'span',
options: {
classes: [ 'letter', 'space' ],
html: ' '
}
})
}
return children
}, [])
}
}
}), {
empty: true
})
}
const createEl = (tag, options) => {
if (typeof tag !== 'string') {
options = tag.options
tag = tag.tag
}
let opts = {
id: null,
class: null,
classes: [],
props: {},
attrs: {},
data: {},
text: null,
html: null,
parent: null,
children: [],
...options
}
let el = document.createElement(tag)
if (opts.id) el.id = opts.id
if (opts.class) el.className = opts.class
if (opts.classes) el.classList.add(...opts.classes)
Object.keys(opts.props).forEach(prop => el[prop] = opts.props[prop])
Object.entries(opts.attrs).forEach(attr => el.setAttribute.call(el, attr))
Object.assign(el.dataset, opts.data)
if (opts.text) el.textContent = opts.text
if (opts.html) el.innerHTML = opts.html
if (opts.parent) query(opts.parent).appendChild(el)
if (opts.children) appendChildren(el, opts.children)
return el
}
const appendChildren = (el, children, options) => {
let opts = {
empty: false,
...options
}
el = el == null ? document.body : query(el)
if (opts.empty) emptyEl(el)
children.forEach(child => {
if (isDomEntity(child)) {
el.appendChild(child)
} else {
let childEl = createEl(child.tag, child.options)
if (child.options.parent == null) {
el.appendChild(childEl)
}
}
})
}
const query = (selector) => {
return typeof selector === 'string' ?
document.querySelector(selector) : selector
}
const emptyEl = el => {
while (el.firstChild) {
el.firstChild.remove()
}
return el
}
const isDomEntity = entity => {
return typeof entity === 'object' && entity.nodeType !== undefined
}
main()
.page {
width: 20em;
overflow: hidden;
border: thin solid grey;
}
.paragraph {
margin: 0.33em 0.5em;
}
.word {
display: inline-block;
}
.word:hover {
background: #FFA;
}
<div class="page">Loading...</div>