0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-07 16:22:42 +00:00

Store livescrolling in local storage

Small fixes for loading the correct current state.
This commit is contained in:
David Bolack
2024-06-03 02:26:56 -05:00
parent e69132b40a
commit a715c9e1e6
3 changed files with 35 additions and 22 deletions

View File

@@ -23,7 +23,6 @@ const DEFAULT_STYLE_TEXT = dedent`
}`; }`;
let lastPage = 0; let lastPage = 0;
let liveScroll = true;
const isElementCodeMirror=(element)=>{ const isElementCodeMirror=(element)=>{
let el = element; let el = element;
@@ -44,7 +43,6 @@ const Editor = createClass({
text : '', text : '',
style : '' style : ''
}, },
liveScroll : true,
onTextChange : ()=>{}, onTextChange : ()=>{},
onStyleChange : ()=>{}, onStyleChange : ()=>{},
@@ -67,12 +65,13 @@ const Editor = createClass({
isMeta : function() {return this.state.view == 'meta';}, isMeta : function() {return this.state.view == 'meta';},
componentDidMount : function() { componentDidMount : function() {
this.updateEditorSize(); this.updateEditorSize();
this.highlightCustomMarkdown(); this.highlightCustomMarkdown();
window.addEventListener('resize', this.updateEditorSize); window.addEventListener('resize', this.updateEditorSize);
document.addEventListener('keydown', this.handleControlKeys); document.addEventListener('keydown', this.handleControlKeys);
document.addEventListener('click', (e)=>{ document.addEventListener('click', (e)=>{
if(isElementCodeMirror(e.target) && liveScroll ) { if(isElementCodeMirror(e.target) && this.props.liveScroll ) {
const curPage = this.getCurrentPage(); const curPage = this.getCurrentPage();
if( curPage != lastPage ) { if( curPage != lastPage ) {
this.brewJump(); this.brewJump();
@@ -94,10 +93,6 @@ const Editor = createClass({
}, },
componentDidUpdate : function(prevProps, prevState, snapshot) { componentDidUpdate : function(prevProps, prevState, snapshot) {
console.log(this.props);
console.log(prevProps);
console.log(this.state);
console.log(prevState);
this.highlightCustomMarkdown(); this.highlightCustomMarkdown();
if(prevProps.moveBrew !== this.props.moveBrew) { if(prevProps.moveBrew !== this.props.moveBrew) {
@@ -106,13 +101,13 @@ const Editor = createClass({
if(prevProps.moveSource !== this.props.moveSource) { if(prevProps.moveSource !== this.props.moveSource) {
this.sourceJump(); this.sourceJump();
}; };
if(prevProps.liveScroll !== this.props.liveScroll) { if(prevProps.liveScroll != this.props.liveScroll) {
liveScroll = !liveScroll; if (this.props.liveScroll) this.brewJump();
}; };
}, },
handleControlKeys : function(e){ handleControlKeys : function(e){
if(liveScroll) { if(this.props.liveScroll) {
const movementKeys = [ 13, 33, 34, 37, 38, 39, 40 ]; const movementKeys = [ 13, 33, 34, 37, 38, 39, 40 ];
if (movementKeys.includes(e.keyCode)) { if (movementKeys.includes(e.keyCode)) {
const curPage = this.getCurrentPage(); const curPage = this.getCurrentPage();
@@ -122,14 +117,14 @@ const Editor = createClass({
} }
} }
} }
const M_KEY = 77; const X_KEY = 88;
const END_KEY = 35; const END_KEY = 35;
const HOME_KEY = 36; const HOME_KEY = 36;
const SCROLLLOCK_KEY = 145; const SCROLLLOCK_KEY = 145;
// Toggle Live-scrolling on Scroll Lock // Toggle Live-scrolling on Scroll Lock
if(e.keyCode == SCROLLLOCK_KEY) { if(e.keyCode == SCROLLLOCK_KEY) {
liveScroll = !liveScroll; this.setState( {liveScroll: !liveScroll} );
} }
if(!(e.ctrlKey || e.metaKey)) return; if(!(e.ctrlKey || e.metaKey)) return;
@@ -140,9 +135,9 @@ const Editor = createClass({
// Brew Jump on CTRL-J // Brew Jump on CTRL-J
if((!e.shiftKey) && (e.keyCode == M_KEY)) this.brewJump(); if((!e.shiftKey) && (e.keyCode == M_KEY)) this.brewJump();
// Source Jump on Shift-CTRL-J // Source Jump on Shift-CTRL-J
if ((e.shiftKey) && (!e.altKey) && (e.keyCode == M_KEY)) this.sourceJump(); if ((e.shiftKey) && (!e.altKey) && (e.keyCode == X_KEY)) this.sourceJump();
if( e.keyCode == M_KEY) { if( e.keyCode == X_KEY) {
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
} }
@@ -330,6 +325,7 @@ const Editor = createClass({
// console.log(`Scroll to: p${targetPage}`); // console.log(`Scroll to: p${targetPage}`);
const brewRenderer = window.frames['BrewRenderer'].contentDocument.getElementsByClassName('brewRenderer')[0]; const brewRenderer = window.frames['BrewRenderer'].contentDocument.getElementsByClassName('brewRenderer')[0];
const currentPos = brewRenderer.scrollTop; const currentPos = brewRenderer.scrollTop;
if(!window.frames['BrewRenderer'].contentDocument.getElementById(`p${targetPage}`)) return;
const targetPos = window.frames['BrewRenderer'].contentDocument.getElementById(`p${targetPage}`).getBoundingClientRect().top; const targetPos = window.frames['BrewRenderer'].contentDocument.getElementById(`p${targetPage}`).getBoundingClientRect().top;
const interimPos = targetPos >= 0 ? -30 : 30; const interimPos = targetPos >= 0 ? -30 : 30;

View File

@@ -20,7 +20,6 @@ const SplitPane = createClass({
isDragging : false, isDragging : false,
moveSource : false, moveSource : false,
moveBrew : false, moveBrew : false,
liveScroll : true,
viewablePageNumber : 0, viewablePageNumber : 0,
showMoveArrows : true showMoveArrows : true
}; };
@@ -41,9 +40,25 @@ const SplitPane = createClass({
}); });
} }
window.addEventListener('resize', this.handleWindowResize); window.addEventListener('resize', this.handleWindowResize);
// This lives here instead of in the initial render because you cannot touch localStorage until the componant mounts.
const loadLiveScroll = window.localStorage.getItem('liveScroll') === 'true';
this.setState({
liveScroll : loadLiveScroll
});
const toggle = document.getElementById('scrollToggle');
const toggleDiv = document.getElementById('scrollToggleDiv');
if(loadLiveScroll) {
toggle.className = 'fas fa-lock';
toggleDiv.className = 'arrow lock';
} else {
toggle.className = 'fas fa-unlock';
toggleDiv.className = 'arrow unlock';
}
}, },
componentWillUnmount : function() { componentWillUnmount : function() {
window.removeEventListener('resize', this.handleWindowResize); window.removeEventListener('resize', this.handleWindowResize);
}, },
@@ -119,21 +134,23 @@ componentWillUnmount : function() {
onClick={()=>this.setState({ moveBrew: !this.state.moveBrew })} > onClick={()=>this.setState({ moveBrew: !this.state.moveBrew })} >
<i className='fas fa-arrow-right' /> <i className='fas fa-arrow-right' />
</div> </div>
<div id='scrollToggleDiv' className='arrow unlock' <div id='scrollToggleDiv' className={`arrow lock`}
style={{ left: this.state.currentDividerPos-4 }} style={{ left: this.state.currentDividerPos-4 }}
onClick={()=>{ onClick={()=>{
this.setState({ liveScroll: !this.state.liveScroll }); const flipLiveScroll = !this.state.liveScroll;
const toggle = document.getElementById('scrollToggle'); const toggle = document.getElementById('scrollToggle');
const toggleDiv = document.getElementById('scrollToggleDiv'); const toggleDiv = document.getElementById('scrollToggleDiv');
if(toggle.classList.contains('fa-unlock')) { if(flipLiveScroll) {
toggle.className = 'fas fa-lock'; toggle.className = 'fas fa-lock';
toggleDiv.className = 'arrow lock'; toggleDiv.className = 'arrow lock';
} else { } else {
toggle.className = 'fas fa-unlock'; toggle.className = 'fas fa-unlock';
toggleDiv.className = 'arrow unlock'; toggleDiv.className = 'arrow unlock';
} }
window.localStorage.setItem('liveScroll', String(flipLiveScroll));
this.setState({ liveScroll: flipLiveScroll });
}} > }} >
<i id='scrollToggle' className='fas fa-unlock' /> <i id='scrollToggle' className={`fas fa-lock`} />
</div> </div>
</>; </>;
} }

View File

@@ -54,11 +54,11 @@
top : 60px; top : 60px;
} }
&.lock{ &.lock{
.tooltipRight('Lock cursor tracking between windows.'); .tooltipRight('Unlock cursor tracking between windows.');
top : 90px; top : 90px;
} }
&.unlock{ &.unlock{
.tooltipRight('Unlock cursor tracking between windows.'); .tooltipRight('Lock cursor tracking between windows.');
top : 90px; top : 90px;
} }
&:hover{ &:hover{