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

adds live page preview on hover :) #52

Merged
merged 34 commits into from May 17, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e1e5c57
adds page preview on hover :)
May 10, 2020
9c06df6
disable preview on toggle
May 11, 2020
b9ee395
use popper for positioning
May 12, 2020
c16e1ba
remove popper if item is removed from dom
May 12, 2020
9c60b20
set scrollTop to 0 for next hover
May 12, 2020
fc3a3d3
support page-ref-tag
May 12, 2020
8a7183e
- disable by default;
May 15, 2020
62d7a67
add warning tooltip, uses react-tooltip-popper
May 15, 2020
53f03d2
rename warningTooltip -> warning
May 16, 2020
db31638
fix: reducer
May 16, 2020
dcfc937
refactor to PreviewIframe class
May 16, 2020
806203d
refactor Tooltip
May 16, 2020
79d4dca
revert manifest.json
May 16, 2020
282f915
clear instance on disable
May 16, 2020
3129b48
Merge commit 'c9593ccce94a31f18bc60b2acbfac5f10b7b7a5e' into live-pre…
May 16, 2020
e29481d
chore: private functions
May 16, 2020
6740221
move initial state to function
May 16, 2020
99338a9
add comment for hack
May 16, 2020
02c9db7
use daily notes page for first load
May 16, 2020
cece075
rename function
May 16, 2020
eabd431
Merge commit 'a955330ee4859b3c7dc88b1bf3bb8cbd0c845aa7' into live-pre…
May 16, 2020
bb08eae
review changes
May 16, 2020
ea574cd
fix comment
May 16, 2020
53459da
fix: condition
May 16, 2020
b64069e
remove log
May 16, 2020
cc7e463
fix: mouse handling
May 16, 2020
13af95d
remove body check
May 16, 2020
5a8d720
review
May 16, 2020
9afd28e
fix: create iframe in constructor,
May 17, 2020
7210520
fix: navigation function
May 17, 2020
73004cf
fix: type
May 17, 2020
f01d18e
Merge remote-tracking branch 'origin/master' into live-preview
May 17, 2020
caff0e7
move live-preview file, add to features
May 17, 2020
aa798b7
remove exisitingIframe check,
May 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 1 addition & 12 deletions src/manifest.json
Expand Up @@ -34,18 +34,7 @@
},
"content_scripts": [
{
"js": [
"shortcuts.js",
"navigation.js",
"srs.js",
"inc-dec-value.js",
"dispatcher.js",
"block-manipulation.js",
"live-preview.js",
"custom-css.js",
"fuzzy_date.js",
"create-block-demo.js"
],
"js": ["entry.js"],
"matches": ["*://roamresearch.com/*"]
}
]
Expand Down
6 changes: 3 additions & 3 deletions src/ts/features/features.ts
Expand Up @@ -4,9 +4,9 @@ import {config as incDec} from './inc-dec-value'
import {config as customCss} from './custom-css'
import {config as srs} from '../srs/srs'
import {config as blockManipulation} from './block-manipulation'
import {config as livePreview} from './live-preview'
import {config as estimate} from './estimates/index'
import {config as navigation} from './navigation/index'
import {config as estimate} from './estimates'
import {config as navigation} from './navigation'
import {config as livePreview} from './livePreview'
import {filterAsync, mapAsync} from '../utils/async'

export const Features = {
Expand Down
@@ -1,5 +1,5 @@
import {Feature, Settings} from '../../utils/settings'
import {Navigation} from '../../roam/navigation'
import {Feature, Settings} from '../utils/settings'
import {Navigation} from '../roam/navigation'
import {browser} from 'webextension-polyfill-ts'
import {createPopper, Instance} from '@popperjs/core'

Expand All @@ -25,12 +25,16 @@ browser.runtime.onMessage.addListener(async message => {
let iframeInstance: PreviewIframe | null = null

const toggleIframe = (active: boolean) => {
if (active && !iframeInstance) {
iframeInstance = new PreviewIframe()
iframeInstance.activate()
} else if (iframeInstance) {
iframeInstance.destroy()
iframeInstance = null
if (active) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e. why change from previous version of this

if (!iframeInstance) {
iframeInstance = new PreviewIframe()
iframeInstance.activate()
}
} else {
if (iframeInstance) {
iframeInstance.destroy()
iframeInstance = null
}
}
}

Expand Down Expand Up @@ -68,23 +72,12 @@ class PreviewIframe {

document.body.removeChild(this.iframe)
}
/**
* HACK: needed because the instance is created thrice onload/toggle.
* Remove this check to see the issue
*/
private getExisitingIframe(): HTMLIFrameElement | null {
return document.getElementById(this.iframeId) ? this.iframe : null
}

private initPreviewIframe() {
const url = Navigation.getDailyNotesUrl()
const existingIframe = this.getExisitingIframe()
if (existingIframe) {
return
}
this.setupHiddenIframe(url)
this.addIframeToBody()
this.scrollToTopHack()
this.scrollToTopOnPageLoad()
this.attachMouseListeners()
}

Expand Down Expand Up @@ -166,23 +159,13 @@ class PreviewIframe {
}

private resetIframeForNextHover() {
this.scrollToTopOnMouseOut()
this.scrollIframeToTopOnMouseOut()
this.iframe.style.pointerEvents = 'none'
this.iframe.style.opacity = '0'
this.iframe.style.height = '0'
this.iframe.style.width = '0'
}

private scrollToTopOnMouseOut() {
if (this.iframe.contentDocument) {
// scroll to top when removed, so the next popup is not scrolled
const scrollContainer = this.iframe.contentDocument.querySelector('.roam-center > div')
if (scrollContainer) {
scrollContainer.scrollTop = 0
}
}
}

private showPreview() {
this.iframe.style.opacity = '1'
this.iframe.style.pointerEvents = 'all'
Expand Down Expand Up @@ -251,17 +234,31 @@ class PreviewIframe {
;(event.target as HTMLIFrameElement).contentDocument?.body.appendChild(styleNode)
}
}
/**
* Sets the iframe's scrollTop to 0,
* so the next popup is in the correct scroll position
*/
private scrollIframeToTopOnMouseOut() {
if (this.iframe.contentDocument) {
const scrollContainer = this.iframe.contentDocument.querySelector('.roam-center > div')
this.scrollToTopForElement(scrollContainer)
}
}

/**
* HACK: to reset scroll after adding iframe to DOM.
* Since the `overflow` is not set to `hidden` for the HTML tag,
* on adding iframe, the body scrolls down,
* causing the loader to not be centerd.
* This fixes it by setting the scrollTop to 0
*/
private scrollToTopHack = () => {
const htmlElement = document.querySelector('html')
if (htmlElement) {
htmlElement.scrollTop = 0
private scrollToTopOnPageLoad = () => {
this.scrollToTopForElement(document.querySelector('html'))
}

private scrollToTopForElement = (element: Element | null) => {
if (element) {
element.scrollTop = 0
}
}
}