This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(preview): move all react dependent stuff in react-preview file
- Loading branch information
Showing
5 changed files
with
182 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import * as React from 'react'; | ||
import * as ReactDom from 'react-dom'; | ||
|
||
import { PreviewApp } from './preview'; | ||
import { HighlightElementFunction } from '../../preview'; | ||
import { Store } from '../../../store/store'; | ||
|
||
export const renderReact = (store: Store, highlightElement: HighlightElementFunction) => { | ||
ReactDom.render( | ||
<PreviewApp store={store} highlightElement={highlightElement} />, | ||
document.getElementById('app') | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { ipcRenderer } from 'electron'; | ||
import { JsonObject } from '../store/json'; | ||
import { renderReact } from './presentation/react/render'; | ||
import * as SmoothscrollPolyfill from 'smoothscroll-polyfill'; | ||
import { Store } from '../store/store'; | ||
|
||
export interface HighlightAreaProps extends ClientRect { | ||
opacity?: number; | ||
} | ||
|
||
export type HighlightElementFunction = ( | ||
element: Element, | ||
currentHighlightArea: HighlightAreaProps, | ||
callback: (newHighlightArea: HighlightAreaProps) => void | ||
) => void; | ||
|
||
const store = new Store(); | ||
|
||
ipcRenderer.on('page-change', (event: {}, message: JsonObject) => { | ||
store.setPageFromJsonInternal(message.page as JsonObject, message.pageId as string); | ||
}); | ||
|
||
ipcRenderer.on('open-styleguide', (event: {}, message: JsonObject) => { | ||
store.openStyleguide(message.styleGuidePath as string); | ||
}); | ||
|
||
ipcRenderer.on('selectedElement-change', (event: {}, message: JsonObject) => { | ||
store.setSelectedElementById(message.selectedElementId as number[]); | ||
}); | ||
|
||
const highlightElement: HighlightElementFunction = (element, currentHighlightArea, callback) => { | ||
const clientRect: ClientRect = element.getBoundingClientRect(); | ||
const newHighlightArea: HighlightAreaProps = { | ||
bottom: clientRect.bottom, | ||
height: clientRect.height, | ||
left: clientRect.left + window.scrollX, | ||
opacity: 1, | ||
right: clientRect.right, | ||
top: clientRect.top + window.scrollY, | ||
width: clientRect.width | ||
}; | ||
|
||
if ( | ||
newHighlightArea.top === currentHighlightArea.top && | ||
newHighlightArea.right === currentHighlightArea.right && | ||
newHighlightArea.bottom === currentHighlightArea.bottom && | ||
newHighlightArea.left === currentHighlightArea.left && | ||
newHighlightArea.height === currentHighlightArea.height && | ||
newHighlightArea.width === currentHighlightArea.width | ||
) { | ||
return; | ||
} | ||
|
||
element.scrollIntoView({ | ||
behavior: 'smooth', | ||
block: 'center', | ||
inline: 'nearest' | ||
}); | ||
|
||
setTimeout(() => hideHighlightArea(newHighlightArea, callback), 500); | ||
callback(newHighlightArea); | ||
}; | ||
|
||
function hideHighlightArea( | ||
highlightArea: HighlightAreaProps, | ||
callback: (highlightArea: HighlightAreaProps) => void | ||
): void { | ||
const newHighlightArea = highlightArea; | ||
newHighlightArea.opacity = 0; | ||
|
||
callback(newHighlightArea); | ||
} | ||
|
||
window.onload = () => { | ||
SmoothscrollPolyfill.polyfill(); | ||
renderReact(store, highlightElement); | ||
}; | ||
|
||
// Disable drag and drop from outside the application | ||
document.addEventListener( | ||
'dragover', | ||
event => { | ||
event.preventDefault(); | ||
}, | ||
false | ||
); | ||
document.addEventListener( | ||
'drop', | ||
event => { | ||
event.preventDefault(); | ||
}, | ||
false | ||
); |
This file was deleted.
Oops, something went wrong.