2022-06-10 05:41:09 -04:00
|
|
|
import { isEqual, cloneDeep } from 'lodash'
|
2020-09-01 04:09:04 -04:00
|
|
|
import './controllers/outline-controller'
|
|
|
|
import { matchOutline, nestOutline } from './outline-parser'
|
2020-06-29 09:05:08 -04:00
|
|
|
import isValidTeXFile from '../../main/is-valid-tex-file'
|
|
|
|
|
|
|
|
class OutlineManager {
|
|
|
|
constructor(ide, scope) {
|
|
|
|
this.ide = ide
|
|
|
|
this.scope = scope
|
|
|
|
this.shareJsDoc = null
|
|
|
|
this.isTexFile = false
|
2020-07-28 05:37:46 -04:00
|
|
|
this.flatOutline = []
|
2022-06-10 05:41:09 -04:00
|
|
|
this.previousFlatOutline = []
|
2020-06-29 09:05:08 -04:00
|
|
|
this.outline = []
|
2020-07-28 05:37:46 -04:00
|
|
|
this.highlightedLine = null
|
|
|
|
this.ignoreNextScroll = false
|
|
|
|
this.ignoreNextCursorUpdate = false
|
2020-06-29 09:05:08 -04:00
|
|
|
|
2022-11-03 08:43:59 -04:00
|
|
|
scope.$watch('editor.newSourceEditor', (now, before) => {
|
|
|
|
if (before && !now) {
|
|
|
|
this.updateOutline()
|
|
|
|
this.broadcastChangeEvent()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-18 09:08:49 -04:00
|
|
|
scope.$on('doc:after-opened', (ev, { isNewDoc }) => {
|
|
|
|
if (isNewDoc) {
|
|
|
|
// if a new doc is opened a cursor updates will be triggered before the
|
|
|
|
// content is loaded. We have to ignore it or the outline highlight
|
|
|
|
// will be incorrect. This doesn't happen when `doc:after-opened` is
|
|
|
|
// fired without a new doc opened.
|
|
|
|
this.ignoreNextCursorUpdate = true
|
|
|
|
}
|
|
|
|
// always ignore the next scroll update so the cursor update takes
|
|
|
|
// precedence
|
2020-07-28 05:37:46 -04:00
|
|
|
this.ignoreNextScroll = true
|
2020-07-16 10:03:51 -04:00
|
|
|
this.shareJsDoc = scope.editor.sharejs_doc
|
2020-06-29 09:05:08 -04:00
|
|
|
this.isTexFile = isValidTeXFile(scope.editor.open_doc_name)
|
|
|
|
this.updateOutline()
|
|
|
|
this.broadcastChangeEvent()
|
|
|
|
})
|
|
|
|
|
|
|
|
scope.$on('doc:changed', () => {
|
|
|
|
this.updateOutline()
|
|
|
|
this.broadcastChangeEvent()
|
|
|
|
})
|
2020-07-28 05:37:46 -04:00
|
|
|
|
2021-06-10 07:26:27 -04:00
|
|
|
scope.$on('file-view:file-opened', () => {
|
|
|
|
this.isTexFile = false
|
|
|
|
this.updateOutline()
|
|
|
|
this.broadcastChangeEvent()
|
|
|
|
})
|
|
|
|
|
2020-07-28 05:37:46 -04:00
|
|
|
scope.$on('cursor:editor:update', (event, cursorPosition) => {
|
|
|
|
if (this.ignoreNextCursorUpdate) {
|
|
|
|
this.ignoreNextCursorUpdate = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.updateHighlightedLine(cursorPosition.row + 1)
|
|
|
|
this.broadcastChangeEvent()
|
|
|
|
})
|
|
|
|
|
|
|
|
scope.$on('scroll:editor:update', (event, middleVisibleRow) => {
|
|
|
|
if (this.ignoreNextScroll) {
|
|
|
|
this.ignoreNextScroll = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateHighlightedLine(middleVisibleRow + 1)
|
|
|
|
})
|
2020-06-29 09:05:08 -04:00
|
|
|
}
|
|
|
|
|
2022-11-03 08:43:59 -04:00
|
|
|
shouldShowOutline() {
|
|
|
|
return !this.scope.editor.newSourceEditor
|
|
|
|
}
|
|
|
|
|
2020-06-29 09:05:08 -04:00
|
|
|
updateOutline() {
|
2022-11-03 08:43:59 -04:00
|
|
|
// Disable if using CM6
|
|
|
|
if (!this.shouldShowOutline()) {
|
|
|
|
return
|
|
|
|
}
|
2020-06-29 09:05:08 -04:00
|
|
|
if (this.isTexFile) {
|
|
|
|
const content = this.ide.editorManager.getCurrentDocValue()
|
|
|
|
if (content) {
|
2020-07-28 05:37:46 -04:00
|
|
|
this.flatOutline = matchOutline(content)
|
2022-06-10 05:41:09 -04:00
|
|
|
if (!isEqual(this.flatOutline, this.previousFlatOutline)) {
|
|
|
|
this.previousFlatOutline = cloneDeep(this.flatOutline)
|
|
|
|
this.outline = nestOutline(this.flatOutline)
|
|
|
|
}
|
|
|
|
return
|
2020-06-29 09:05:08 -04:00
|
|
|
}
|
|
|
|
}
|
2022-06-10 05:41:09 -04:00
|
|
|
this.previousFlatOutline = []
|
|
|
|
this.outline = []
|
2020-06-29 09:05:08 -04:00
|
|
|
}
|
|
|
|
|
2020-07-28 05:37:46 -04:00
|
|
|
// set highlightedLine to the closest outline line above the editorLine
|
|
|
|
updateHighlightedLine(editorLine) {
|
|
|
|
let closestOutlineLine = null
|
|
|
|
for (let lineId = 0; lineId < this.flatOutline.length; lineId++) {
|
|
|
|
const outline = this.flatOutline[lineId]
|
|
|
|
if (editorLine < outline.line) break // editorLine is above
|
|
|
|
closestOutlineLine = outline.line
|
|
|
|
}
|
2020-08-06 05:04:45 -04:00
|
|
|
if (closestOutlineLine === this.highlightedLine) return
|
2020-07-28 05:37:46 -04:00
|
|
|
this.highlightedLine = closestOutlineLine
|
2020-08-06 05:04:45 -04:00
|
|
|
this.broadcastChangeEvent()
|
2020-07-28 05:37:46 -04:00
|
|
|
}
|
|
|
|
|
2020-08-24 08:20:54 -04:00
|
|
|
jumpToLine(line, syncToPdf) {
|
2020-07-28 05:37:46 -04:00
|
|
|
this.ignoreNextScroll = true
|
2020-08-24 08:20:54 -04:00
|
|
|
this.ide.editorManager.jumpToLine({ gotoLine: line, syncToPdf })
|
2020-06-29 09:05:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
broadcastChangeEvent() {
|
2022-11-03 08:43:59 -04:00
|
|
|
// Disable if using CM6
|
|
|
|
if (!this.shouldShowOutline()) {
|
|
|
|
return
|
|
|
|
}
|
2020-06-29 09:05:08 -04:00
|
|
|
this.scope.$broadcast('outline-manager:outline-changed', {
|
|
|
|
isTexFile: this.isTexFile,
|
2020-07-28 05:37:46 -04:00
|
|
|
outline: this.outline,
|
2021-04-27 03:52:58 -04:00
|
|
|
highlightedLine: this.highlightedLine,
|
2020-06-29 09:05:08 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default OutlineManager
|