Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hide header automatically #76

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/viewer/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Header {

this.draw()
if (!document.getElementById('hwpjs-header-css')) {
const buttonStyle: string = `
const headerStyle: string = `
.hwpjs-header-control {
transition: background .2s;
}
Expand All @@ -71,11 +71,17 @@ class Header {
.hwpjs-header-control:active {
background: #AAA;
}
.hwpjs-header {
transition: margin-top 0.5s;
}
.hwpjs-header--hide .hwpjs-header {
margin-top: -35px;
}
`
const styleSheet: HTMLStyleElement = document.createElement('style')
styleSheet.type = 'text/css'
styleSheet.id = 'hwpjs-header-css'
styleSheet.innerText = buttonStyle
styleSheet.innerText = headerStyle
document.head.appendChild(styleSheet)
}
}
Expand Down Expand Up @@ -123,6 +129,7 @@ class Header {
content.style.width = '100%'
content.style.padding = '0 24px'

header.classList.add('hwpjs-header')
header.appendChild(content)
container.appendChild(header)

Expand Down
50 changes: 50 additions & 0 deletions packages/viewer/src/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ const TEXT_ALIGN: { [key: number]: string } = {
3: 'center',
}

interface focusInfo {
focus: boolean;
lastTime: number;
headerShown: boolean;
}

class HWPViewer {
private hwpDocument: HWPDocument

Expand All @@ -80,10 +86,53 @@ class HWPViewer {

private header: Header | null = null

private headerFocusInfo: focusInfo = { focus: false, lastTime: Date.now(), headerShown: true }

constructor(container: HTMLElement, data: Uint8Array, option: CFB$ParsingOptions = { type: 'binary' }) {
this.container = container
this.hwpDocument = parsePage(parse(data, option))
this.draw()
if (!document.getElementById('hwpjs-css')) {
const viewerStyle: string = `
.hwpjs-container {
transition: all .5s;
}
.hwpjs-header--hide .hwpjs-container {
margin-top: 0 !important;
height: 100% !important;
}
`
const styleSheet: HTMLStyleElement = document.createElement('style')
styleSheet.type = 'text/css'
styleSheet.id = 'hwpjs-header-css'
styleSheet.innerText = viewerStyle
document.head.appendChild(styleSheet)

this.headerFocusHandler = this.headerFocusHandler.bind(this)
this.headerFocusInterval = this.headerFocusInterval.bind(this)
this.container.addEventListener('mousemove', this.headerFocusHandler, false)
setInterval(this.headerFocusInterval, 100)
}
}

headerFocusHandler(e: MouseEvent) {
const offsetY = e.clientY - (e.currentTarget as HTMLElement).getBoundingClientRect().top
if (offsetY <= 32) {
this.headerFocusInfo.focus = true
this.headerFocusInfo.lastTime = Date.now()
}
else this.headerFocusInfo.focus = false
}

headerFocusInterval() {
let headerShouldShown = false
if (this.headerFocusInfo.focus) headerShouldShown = true
else if (Date.now() - this.headerFocusInfo.lastTime <= 3000) headerShouldShown = true
if(headerShouldShown !== this.headerFocusInfo.headerShown) {
this.headerFocusInfo.headerShown = headerShouldShown
if (headerShouldShown) this.container.classList.remove('hwpjs-header--hide')
else this.container.classList.add('hwpjs-header--hide')
}
}

distory() {
Expand Down Expand Up @@ -369,6 +418,7 @@ class HWPViewer {
content.style.overflow = 'auto'
content.style.position = 'relative'
content.style.zIndex = '0'
content.classList.add('hwpjs-container')

this.hwpDocument.sections.forEach((section, index) => {
this.drawSection(content, section, index)
Expand Down