Skip to content

Commit

Permalink
Enable strictNullChecks for viewer/**/*.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Apr 28, 2020
1 parent 8dffe2e commit 9cdc1e7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"one-var": ["error", { "initialized": "never", "uninitialized": "never" }],
"prefer-const": "error",
"quotes": ["error", "single", {"avoidEscape": true}],
"@typescript-eslint/semi": ["error", "never"],
"@typescript-eslint/semi": ["error", "never", { "beforeStatementContinuationChars": "always" }],
"@typescript-eslint/member-delimiter-style": ["error", {
"multiline": {"delimiter": "comma", "requireLast": false},
"singleline": {"delimiter": "comma", "requireLast": false}
Expand Down
14 changes: 7 additions & 7 deletions viewer/components/pagetrimmer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function getTrimScale() {
}


document.getElementById('trimSelect').addEventListener('change', () => {
(document.getElementById('trimSelect') as HTMLElement).addEventListener('change', () => {
const trimScale = getTrimScale()
const trimSelect = document.getElementById('trimSelect') as HTMLSelectElement
const scaleSelect = document.getElementById('scaleSelect') as HTMLSelectElement
Expand All @@ -24,8 +24,8 @@ document.getElementById('trimSelect').addEventListener('change', () => {
for ( const opt of scaleSelect.options ) {
opt.disabled = false
}
(document.getElementById('trimOption') as HTMLOptionElement).disabled = true
document.getElementById('trimOption').hidden = true
(document.getElementById('trimOption') as HTMLOptionElement).disabled = true;
(document.getElementById('trimOption') as HTMLOptionElement).hidden = true
if (originalUserSelectIndex !== undefined) {
/**
* If the original scale is custom, selectedIndex === 4,
Expand All @@ -41,7 +41,7 @@ document.getElementById('trimSelect').addEventListener('change', () => {
scaleSelect.dispatchEvent(ev)
currentUserSelectScale = undefined
originalUserSelectIndex = undefined
const viewer = document.getElementById('viewer')
const viewer = document.getElementById('viewer') as HTMLElement
for ( const page of viewer.getElementsByClassName('page') ) {
for ( const layer of page.getElementsByClassName('annotationLayer') ) {
for ( const secionOfAnnotation of layer.getElementsByTagName('section') ) {
Expand Down Expand Up @@ -124,7 +124,7 @@ function setObserverToTrim() {
trimPage(page)
})
})
const viewer = document.getElementById('viewer')
const viewer = document.getElementById('viewer') as HTMLElement
for( const page of viewer.getElementsByClassName('page') as HTMLCollectionOf<HTMLElement> ){
if (page.dataset.isObserved !== 'observed') {
observer.observe(page, {attributes: true, childList: true, attributeFilter: ['style']})
Expand Down Expand Up @@ -161,7 +161,7 @@ export class PageTrimmer {
}, {once: true})

this.lwApp.onDidRenderPdfFile( () => {
const container = document.getElementById('trimSelectContainer')
const container = document.getElementById('trimSelectContainer') as HTMLElement
const select = document.getElementById('trimSelect') as HTMLSelectElement

// tweak UI https://github.com/James-Yu/LaTeX-Workshop/pull/979
Expand All @@ -176,7 +176,7 @@ export class PageTrimmer {
if (select.selectedIndex <= 0) {
return
}
const viewer = document.getElementById('viewer')
const viewer = document.getElementById('viewer') as HTMLElement
for( const page of viewer.getElementsByClassName('page') as HTMLCollectionOf<HTMLElement> ){
trimPage(page)
}
Expand Down
10 changes: 6 additions & 4 deletions viewer/components/synctex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export class SyncTex {
// workaround for https://github.com/James-Yu/LaTeX-Workshop/issues/1314
if(selection && selection.anchorNode && selection.anchorNode.nodeName === '#text'){
const text = selection.anchorNode.textContent
textBeforeSelection = text.substring(0, selection.anchorOffset)
textAfterSelection = text.substring(selection.anchorOffset)
if (text) {
textBeforeSelection = text.substring(0, selection.anchorOffset)
textAfterSelection = text.substring(selection.anchorOffset)
}
}
const trimSelect = document.getElementById('trimSelect') as HTMLSelectElement
let left = e.pageX - pageDom.offsetLeft + viewerContainer.scrollLeft
Expand All @@ -40,10 +42,10 @@ export class SyncTex {

registerListenerOnEachPage() {
const keybinding = this.reverseSynctexKeybinding
const viewerDom = document.getElementById('viewer')
const viewerDom = document.getElementById('viewer') as HTMLElement
for (const pageDom of viewerDom.childNodes as NodeListOf<HTMLElement>) {
const page = Number(pageDom.dataset.pageNumber)
const viewerContainer = document.getElementById('viewerContainer')
const viewerContainer = document.getElementById('viewerContainer') as HTMLElement
switch (keybinding) {
case 'ctrl-click': {
pageDom.onclick = (e) => {
Expand Down
24 changes: 15 additions & 9 deletions viewer/components/viewerhistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ export class ViewerHistory {

registerKeybinding() {
const setHistory = () => {
const container = document.getElementById('viewerContainer')
const container = document.getElementById('viewerContainer') as HTMLElement
// set positions before and after clicking to viewerHistory
this.lwApp.viewerHistory.set(container.scrollTop)
setTimeout(() => {this.lwApp.viewerHistory.set(container.scrollTop)}, 500)
}
};

document.getElementById('viewerContainer').addEventListener('click', setHistory)
document.getElementById('sidebarContainer').addEventListener('click', setHistory)
(document.getElementById('viewerContainer') as HTMLElement).addEventListener('click', setHistory);
(document.getElementById('sidebarContainer') as HTMLElement).addEventListener('click', setHistory);

// back button (mostly useful for the embedded viewer)
document.getElementById('historyBack').addEventListener('click', () => {
(document.getElementById('historyBack') as HTMLElement).addEventListener('click', () => {
this.lwApp.viewerHistory.back()
})
});

document.getElementById('historyForward').addEventListener('click', () => {
(document.getElementById('historyForward') as HTMLElement).addEventListener('click', () => {
this.lwApp.viewerHistory.forward()
})
}
Expand Down Expand Up @@ -79,8 +79,11 @@ export class ViewerHistory {
if (this.length() === 0) {
return
}
const container = document.getElementById('viewerContainer')
const container = document.getElementById('viewerContainer') as HTMLElement
let cur = this.currentIndex
if (cur === undefined) {
return
}
let prevScroll = this.history[cur].scroll
if (this.length() > 0 && prevScroll !== container.scrollTop) {
if (this.currentIndex === this.lastIndex() && this.last()) {
Expand Down Expand Up @@ -111,8 +114,11 @@ export class ViewerHistory {
if (this.currentIndex === this.lastIndex()) {
return
}
const container = document.getElementById('viewerContainer')
const container = document.getElementById('viewerContainer') as HTMLElement
const cur = this.currentIndex
if (cur === undefined) {
return
}
const nextScroll = this.history[cur+1].scroll
if (nextScroll !== container.scrollTop) {
this.currentIndex = cur + 1
Expand Down
30 changes: 15 additions & 15 deletions viewer/latexworkshop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class LateXWorkshopPdfViewer implements ILatexWorkshopPdfViewer {

const pack = this.decodeQuery()
this.encodedPdfFilePath = pack.encodedPdfFilePath
this.documentTitle = pack.documentTitle
this.documentTitle = pack.documentTitle || ''
document.title = this.documentTitle
this.pdfFilePath = pack.pdfFilePath

Expand Down Expand Up @@ -102,7 +102,7 @@ class LateXWorkshopPdfViewer implements ILatexWorkshopPdfViewer {
switch (data.type) {
case 'synctex': {
// use the offsetTop of the actual page, much more accurate than multiplying the offsetHeight of the first page
const container = document.getElementById('viewerContainer')
const container = document.getElementById('viewerContainer') as HTMLElement
const pos = PDFViewerApplication.pdfViewer._pages[data.data.page - 1].viewport.convertToViewportPoint(data.data.x, data.data.y)
const page = document.getElementsByClassName('page')[data.data.page - 1] as HTMLElement
const scrollX = page.offsetLeft + pos[0]
Expand All @@ -113,7 +113,7 @@ class LateXWorkshopPdfViewer implements ILatexWorkshopPdfViewer {
container.scrollTop = scrollY - document.body.offsetHeight * 0.4
this.viewerHistory.set(container.scrollTop)

const indicator = document.getElementById('synctex-indicator')
const indicator = document.getElementById('synctex-indicator') as HTMLElement
indicator.className = 'show'
indicator.style.left = `${scrollX}px`
indicator.style.top = `${scrollY}px`
Expand All @@ -125,8 +125,8 @@ class LateXWorkshopPdfViewer implements ILatexWorkshopPdfViewer {
scale: PDFViewerApplication.pdfViewer.currentScaleValue,
scrollMode: PDFViewerApplication.pdfViewer.scrollMode,
spreadMode: PDFViewerApplication.pdfViewer.spreadMode,
scrollTop: document.getElementById('viewerContainer').scrollTop,
scrollLeft: document.getElementById('viewerContainer').scrollLeft
scrollTop: (document.getElementById('viewerContainer') as HTMLElement).scrollTop,
scrollLeft: (document.getElementById('viewerContainer') as HTMLElement).scrollLeft
}

// Note: without showPreviousViewOnLoad = false restoring the position after the refresh will fail if
Expand All @@ -147,9 +147,9 @@ class LateXWorkshopPdfViewer implements ILatexWorkshopPdfViewer {
this.onDidLoadPdfFile( () => {
PDFViewerApplication.pdfViewer.currentScaleValue = pack.scale
PDFViewerApplication.pdfViewer.scrollMode = pack.scrollMode
PDFViewerApplication.pdfViewer.spreadMode = pack.spreadMode
document.getElementById('viewerContainer').scrollTop = pack.scrollTop
document.getElementById('viewerContainer').scrollLeft = pack.scrollLeft
PDFViewerApplication.pdfViewer.spreadMode = pack.spreadMode;
(document.getElementById('viewerContainer') as HTMLElement).scrollTop = pack.scrollTop;
(document.getElementById('viewerContainer') as HTMLElement).scrollLeft = pack.scrollLeft
}, {once: true})
this.onDidRenderPdfFile( () => {
this.send({type:'loaded', path:this.pdfFilePath})
Expand Down Expand Up @@ -182,8 +182,8 @@ class LateXWorkshopPdfViewer implements ILatexWorkshopPdfViewer {
}, {once: true})
}
if (data.invert > 0) {
document.querySelector('html').style.filter = `invert(${data.invert * 100}%)`
document.querySelector('html').style.background = 'white'
(document.querySelector('html') as HTMLHtmlElement).style.filter = `invert(${data.invert * 100}%)`;
(document.querySelector('html') as HTMLHtmlElement).style.background = 'white'
}
(document.querySelector('#viewerContainer') as HTMLElement).style.background = data.bgColor
if (data.keybindings) {
Expand All @@ -196,7 +196,7 @@ class LateXWorkshopPdfViewer implements ILatexWorkshopPdfViewer {
this.send( {
type: 'status',
path: this.pdfFilePath,
scrollTop: document.getElementById('viewerContainer').scrollTop
scrollTop: (document.getElementById('viewerContainer') as HTMLElement).scrollTop
})
break
}
Expand Down Expand Up @@ -263,8 +263,8 @@ class LateXWorkshopPdfViewer implements ILatexWorkshopPdfViewer {
const param = parts[i].split('=')
if (param[0].toLowerCase() === 'incode' && param[1] === '1') {
const dom = document.getElementsByClassName('print') as HTMLCollectionOf<HTMLElement>
for (let j = 0; j < dom.length; ++j) {
dom.item(j).style.display='none'
for (const item of dom) {
item.style.display='none'
}
}
}
Expand Down Expand Up @@ -301,9 +301,9 @@ class LateXWorkshopPdfViewer implements ILatexWorkshopPdfViewer {
this.viewerHistory.forward()
}
}
})
});

document.getElementById('outerContainer').onmousemove = (e) => {
(document.getElementById('outerContainer') as HTMLElement).onmousemove = (e) => {
if (e.clientY <= 64) {
this.showToolbar(true)
}
Expand Down
2 changes: 2 additions & 0 deletions viewer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"sourceMap": true,
"strictBindCallApply": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"target": "es2017",
"typeRoots": []
}
Expand Down

0 comments on commit 9cdc1e7

Please sign in to comment.