2017-07-23 18:42:08 +00:00
|
|
|
<template>
|
|
|
|
<div class="toc">
|
2017-09-23 19:01:50 +00:00
|
|
|
<div class="toc__mask" :style="{top: (maskY - 5) + 'px'}"></div>
|
|
|
|
<div class="toc__inner"></div>
|
2017-07-23 18:42:08 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
2017-08-06 00:58:39 +00:00
|
|
|
|
|
|
|
<script>
|
2017-09-23 19:01:50 +00:00
|
|
|
import { mapGetters } from 'vuex';
|
2017-08-06 00:58:39 +00:00
|
|
|
import editorSvc from '../services/editorSvc';
|
|
|
|
|
|
|
|
export default {
|
2017-09-23 19:01:50 +00:00
|
|
|
data: () => ({
|
2017-09-26 22:54:26 +00:00
|
|
|
maskY: 0,
|
2017-09-23 19:01:50 +00:00
|
|
|
}),
|
|
|
|
computed: {
|
|
|
|
...mapGetters('layout', [
|
|
|
|
'styles',
|
|
|
|
]),
|
|
|
|
},
|
2017-08-06 00:58:39 +00:00
|
|
|
mounted() {
|
|
|
|
const tocElt = this.$el.querySelector('.toc__inner');
|
|
|
|
|
|
|
|
// TOC click behaviour
|
|
|
|
let isMousedown;
|
|
|
|
function onClick(e) {
|
|
|
|
if (!isMousedown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
const y = e.clientY - tocElt.getBoundingClientRect().top;
|
|
|
|
|
|
|
|
editorSvc.sectionDescList.some((sectionDesc) => {
|
|
|
|
if (y >= sectionDesc.tocDimension.endOffset) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const posInSection = (y - sectionDesc.tocDimension.startOffset)
|
|
|
|
/ (sectionDesc.tocDimension.height || 1);
|
|
|
|
const editorScrollTop = sectionDesc.editorDimension.startOffset
|
|
|
|
+ (sectionDesc.editorDimension.height * posInSection);
|
|
|
|
editorSvc.editorElt.parentNode.scrollTop = editorScrollTop;
|
|
|
|
const previewScrollTop = sectionDesc.previewDimension.startOffset
|
|
|
|
+ (sectionDesc.previewDimension.height * posInSection);
|
|
|
|
editorSvc.previewElt.parentNode.scrollTop = previewScrollTop;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
tocElt.addEventListener('mouseup', () => {
|
|
|
|
isMousedown = false;
|
|
|
|
});
|
|
|
|
tocElt.addEventListener('mouseleave', () => {
|
|
|
|
isMousedown = false;
|
|
|
|
});
|
|
|
|
tocElt.addEventListener('mousedown', (e) => {
|
|
|
|
isMousedown = e.which === 1;
|
|
|
|
onClick(e);
|
|
|
|
});
|
|
|
|
tocElt.addEventListener('mousemove', (e) => {
|
|
|
|
onClick(e);
|
|
|
|
});
|
2017-09-23 19:01:50 +00:00
|
|
|
|
|
|
|
// Change mask postion on scroll
|
|
|
|
const updateMaskY = () => {
|
|
|
|
const scrollPosition = editorSvc.getScrollPosition();
|
2017-09-26 22:54:26 +00:00
|
|
|
if (scrollPosition) {
|
|
|
|
const sectionDesc = editorSvc.sectionDescList[scrollPosition.sectionIdx];
|
|
|
|
this.maskY = sectionDesc.tocDimension.startOffset +
|
|
|
|
(scrollPosition.posInSection * sectionDesc.tocDimension.height);
|
|
|
|
}
|
2017-09-23 19:01:50 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 23:39:51 +00:00
|
|
|
this.$nextTick(() => {
|
2017-09-23 19:01:50 +00:00
|
|
|
editorSvc.editorElt.parentNode.addEventListener('scroll', () => {
|
|
|
|
if (this.styles.showEditor) {
|
|
|
|
updateMaskY();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
editorSvc.previewElt.parentNode.addEventListener('scroll', () => {
|
|
|
|
if (!this.styles.showEditor) {
|
|
|
|
updateMaskY();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-08-06 00:58:39 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.toc__inner {
|
2018-01-04 20:19:10 +00:00
|
|
|
position: relative;
|
2017-08-06 00:58:39 +00:00
|
|
|
color: rgba(0, 0, 0, 0.75);
|
|
|
|
cursor: pointer;
|
2017-11-17 00:38:55 +00:00
|
|
|
font-size: 9px;
|
2017-09-17 15:32:39 +00:00
|
|
|
padding: 10px 20px 40px;
|
2017-08-06 00:58:39 +00:00
|
|
|
white-space: nowrap;
|
|
|
|
-webkit-user-select: none;
|
|
|
|
-moz-user-select: none;
|
|
|
|
-ms-user-select: none;
|
|
|
|
user-select: none;
|
|
|
|
|
|
|
|
* {
|
|
|
|
font-weight: inherit;
|
|
|
|
pointer-events: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.cl-toc-section {
|
2017-11-04 16:59:48 +00:00
|
|
|
h1,
|
|
|
|
h2 {
|
|
|
|
&::after {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
h1 {
|
|
|
|
margin: 1rem 0;
|
2017-08-06 00:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h2 {
|
2017-11-04 16:59:48 +00:00
|
|
|
margin: 0.5rem 0;
|
2017-08-06 00:58:39 +00:00
|
|
|
margin-left: 8px;
|
|
|
|
}
|
|
|
|
|
|
|
|
h3 {
|
2017-11-04 16:59:48 +00:00
|
|
|
margin: 0.33rem 0;
|
2017-08-06 00:58:39 +00:00
|
|
|
margin-left: 16px;
|
|
|
|
}
|
|
|
|
|
|
|
|
h4 {
|
2017-11-04 16:59:48 +00:00
|
|
|
margin: 0.22rem 0;
|
2017-08-06 00:58:39 +00:00
|
|
|
margin-left: 24px;
|
|
|
|
}
|
|
|
|
|
|
|
|
h5 {
|
2017-11-04 16:59:48 +00:00
|
|
|
margin: 0.11rem 0;
|
2017-08-06 00:58:39 +00:00
|
|
|
margin-left: 32px;
|
|
|
|
}
|
|
|
|
|
|
|
|
h6 {
|
2017-11-04 16:59:48 +00:00
|
|
|
margin: 0;
|
2017-08-06 00:58:39 +00:00
|
|
|
margin-left: 40px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-23 19:01:50 +00:00
|
|
|
|
|
|
|
.toc__mask {
|
|
|
|
position: absolute;
|
|
|
|
left: 0;
|
|
|
|
width: 100%;
|
|
|
|
height: 35px;
|
2018-01-04 20:19:10 +00:00
|
|
|
background-color: rgba(255, 255, 255, 0.2);
|
2017-09-23 19:01:50 +00:00
|
|
|
pointer-events: none;
|
|
|
|
}
|
2017-08-06 00:58:39 +00:00
|
|
|
</style>
|