mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-19 14:12:40 +00:00
add check for change to current viewport
This commit is contained in:
@@ -114,30 +114,40 @@ const Editor = createClass({
|
|||||||
|
|
||||||
let editorPageCount = 2; // start counting from page 2
|
let editorPageCount = 2; // start counting from page 2
|
||||||
|
|
||||||
|
// get top and bottom line numbers currently in editor viewport
|
||||||
|
const viewportRect = codeMirror.getWrapperElement().getBoundingClientRect();
|
||||||
|
const topVisibleLine = codeMirror.lineAtHeight(viewportRect.top, "window");
|
||||||
|
const bottomVisibleLine = codeMirror.lineAtHeight(viewportRect.bottom, "window");
|
||||||
|
|
||||||
_.forEach(this.props.brew.text.split('\n'), (line, lineNumber)=>{
|
_.forEach(this.props.brew.text.split('\n'), (line, lineNumber)=>{
|
||||||
if(this.props.renderer == 'legacy') {
|
if(this.props.renderer == 'legacy') {
|
||||||
if(line.includes('\\page')) {
|
if(line.includes('\\page')) {
|
||||||
const testElement = Object.assign(document.createElement('div'), {
|
// add a check here to see if in current viewport, then create widget if true
|
||||||
className : 'editor-page-count',
|
if(lineNumber > topVisibleLine && lineNumber < bottomVisibleLine){
|
||||||
textContent : editorPageCount
|
const testElement = Object.assign(document.createElement('div'), {
|
||||||
});
|
className : 'editor-page-count',
|
||||||
codeMirror.addWidget({ line: lineNumber, ch: 0 }, testElement);
|
textContent : editorPageCount
|
||||||
// testElement.style.top = `${parseInt(testElement.style.top) - 12.5}px`;
|
});
|
||||||
testElement.style.top = `${parseInt(testElement.style.top) - cm.defaultTextHeight()}px`;
|
codeMirror.addWidget({ line: lineNumber, ch: 0 }, testElement);
|
||||||
testElement.style.left = null;
|
testElement.style.top = `${parseInt(testElement.style.top) - codeMirror.defaultTextHeight()}px`;
|
||||||
|
testElement.style.left = null;
|
||||||
|
}
|
||||||
|
// end createWidget process
|
||||||
editorPageCount = editorPageCount + 1;
|
editorPageCount = editorPageCount + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(this.props.renderer == 'V3') {
|
if(this.props.renderer == 'V3') {
|
||||||
if(line.match(/^\\page$/)){
|
if(line.match(/^\\page$/)){
|
||||||
const testElement = Object.assign(document.createElement('div'), {
|
if(lineNumber > topVisibleLine && lineNumber < bottomVisibleLine){
|
||||||
className : 'editor-page-count',
|
const testElement = Object.assign(document.createElement('div'), {
|
||||||
textContent : editorPageCount
|
className : 'editor-page-count',
|
||||||
});
|
textContent : editorPageCount
|
||||||
codeMirror.addWidget({ line: lineNumber, ch: 0 }, testElement);
|
});
|
||||||
testElement.style.top = `${parseInt(testElement.style.top) - 12.5}px`;
|
codeMirror.addWidget({ line: lineNumber, ch: 0 }, testElement);
|
||||||
testElement.style.left = null;
|
testElement.style.top = `${parseInt(testElement.style.top) - codeMirror.defaultTextHeight()}px`;
|
||||||
|
testElement.style.left = null;
|
||||||
|
}
|
||||||
editorPageCount = editorPageCount + 1;
|
editorPageCount = editorPageCount + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ const CodeEditor = createClass({
|
|||||||
|
|
||||||
// Note: codeMirror passes a copy of itself in this callback. cm === this.codeMirror. Either one works.
|
// Note: codeMirror passes a copy of itself in this callback. cm === this.codeMirror. Either one works.
|
||||||
this.codeMirror.on('change', (cm)=>{this.props.onChange(cm.getValue());});
|
this.codeMirror.on('change', (cm)=>{this.props.onChange(cm.getValue());});
|
||||||
|
this.codeMirror.on('viewportChange', _.debounce((cm)=>{this.props.onChange(cm.getValue());},200));
|
||||||
this.updateSize();
|
this.updateSize();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user