This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Split a paragraph to two paragraphs | |
* @param {HTMLElement} paragraph - paragraph element | |
*/ | |
_splitParagraph(paragraph) { | |
const textNodes = []; | |
const treeWalker = document.createTreeWalker(paragraph); | |
while (treeWalker.nextNode()) { | |
const node = treeWalker.currentNode; | |
if (node.nodeType === Node.TEXT_NODE) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// for keeping the cursor | |
if (range | |
&& range.startContainer === textNode | |
&& range.startOffset === index) { | |
range.setStartBefore(span); | |
range.setEndBefore(span); | |
} | |
... | |
... | |
// keep the cursor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// recognize lines | |
let prevSpan; | |
wrappers.forEach(span => { | |
const prevSpanBottom = prevSpan ? prevSpan.getBoundingClientRect().bottom : 0; | |
const spanTop = span.getBoundingClientRect().top; | |
if (prevSpanBottom < spanTop) { | |
lines.push(span); | |
} | |
prevSpan = span; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// find a exceed first line | |
let nextParagraphCharacters = []; | |
const {length} = lines; | |
for (let i = 0; i < length; i += 1) { | |
const line = lines[i]; | |
const lineBottom = this._getBottom(line); | |
if (lineBottom > pageBodyBottom) { | |
const splitIndex = wrappers.indexOf(line); | |
nextParagraphCharacters = wrappers.slice(splitIndex); | |
break; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// unwrap text nodes | |
wrappers.forEach(span => { | |
if (span.parentElement) { | |
const textNode = span.firstChild; | |
span.removeChild(textNode); | |
span.parentElement.insertBefore(textNode, span); | |
span.parentElement.removeChild(span); | |
} | |
}); | |
// keep the cursor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add event listners to layout pages | |
*/ | |
_addEventListener() { | |
document.addEventListener('keyup', event => { | |
if (event.target.isContentEditable) { | |
this._layout(); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// split the paragraph to two paragraphs | |
const extractRange = document.createRange(); | |
extractRange.setStartBefore(nextParagraphCharacters[0]); | |
extractRange.setEndAfter(nextParagraphCharacters[nextParagraphCharacters.length - 1]); | |
const fragment = extractRange.extractContents(); | |
const nextParagraph = paragraph.cloneNode(); | |
nextParagraph.innerHTML = ''; | |
nextParagraph.appendChild(fragment); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = 10; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const webpack = require('webpack'); | |
const pkg = require('./package.json'); | |
const isProduction = process.env.NODE_ENV === 'production'; | |
const FILENAME = pkg.name + (isProduction ? '.min' : ''); | |
const config = { | |
entry: './src/ts/index.ts', | |
output: { | |
path: path.join(__dirname, 'dist'), | |
filename: FILENAME + '.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default {}; |