diff --git a/src/App.js b/src/App.js index 5c30bc0..35d9d09 100644 --- a/src/App.js +++ b/src/App.js @@ -6,6 +6,12 @@ import getBlock from './blocks'; import getIcons from './icons'; const iconList = getIcons(); +const blockListArr = []; + +Object.entries(iconList).forEach(([type, icons]) => { + Object.keys(icons).map(name => blockListArr.push(`${name},${type}`)); +}); + const themeList = ["indigo", "orange", "teal", "red", "purple", "pink", "blue", "green"]; const desktopIcon = ( @@ -88,6 +94,7 @@ class App extends Component { copied: false, sidebar: true, codeView: false, + currentKeyCode: null, view: 'desktop', theme: 'indigo', blockType: 'Blog', @@ -103,6 +110,7 @@ class App extends Component { this.toggleSidebar = this.toggleSidebar.bind(this); this.toggleView = this.toggleView.bind(this); this.copyToClipboard = this.copyToClipboard.bind(this); + this.keyboardNavigation = this.keyboardNavigation.bind(this); this.markupRef = React.createRef(); this.textareaRef = React.createRef(); this.sidebarRef = React.createRef(); @@ -110,21 +118,12 @@ class App extends Component { } componentDidMount() { - window.focus(); - const iframe = document.querySelector('iframe'); + document.addEventListener('keydown', this.keyboardNavigation); + } + + hideSidebar() { const sidebar = this.sidebarRef.current; const opener = this.openerRef.current; - let iframeMouseOver = false; - - window.focus(); - window.addEventListener('blur', () => { - if (iframeMouseOver) { - this.setState({ sidebar: false }); - } - }); - - iframe.addEventListener('mouseover', () => iframeMouseOver = true); - iframe.addEventListener('mouseout', () => iframeMouseOver = false); document.addEventListener('click', (e) => { if (e.target === opener) { @@ -137,11 +136,77 @@ class App extends Component { }); } + keyboardNavigation(e) { + const { blockType, blockName } = this.state; + const blockStringFormat = `${blockName},${blockType}`; + const keyCode = e.which || e.keyCode; + + switch (keyCode) { + case 40: // Down + e.preventDefault(); + blockListArr.forEach((block, index) => { + if (block === blockStringFormat) { + const newActiveBlock = index + 1 <= blockListArr.length - 1 ? blockListArr[index + 1].split(',') : blockListArr[0].split(','); + const newBlockName = newActiveBlock[0]; + const newBlockType = newActiveBlock[1]; + const newBlockNode = document.querySelector(`.block-item[block-name="${newBlockName}"]`); + if (newBlockNode) newBlockNode.focus(); + this.setState({ + blockType: newBlockType, + blockName: newBlockName, + codeView: false, + currentKeyCode: 40 + }); + } + }); + break; + case 37: // Left + e.preventDefault(); + this.setState({ sidebar: false, currentKeyCode: 37 }); + break; + case 39: // Right + e.preventDefault(); + this.setState({ sidebar: true, currentKeyCode: 39 }); + break; + case 38: // Up + e.preventDefault(); + blockListArr.forEach((block, index) => { + if (block === blockStringFormat) { + const newActiveBlock = index - 1 >= 0 ? blockListArr[index - 1].split(',') : blockListArr[blockListArr.length - 1].split(','); + const newBlockName = newActiveBlock[0]; + const newBlockType = newActiveBlock[1]; + const newBlockNode = document.querySelector(`.block-item[block-name="${newBlockName}"]`); + if (newBlockNode) newBlockNode.focus(); + + this.setState({ + blockType: newBlockType, + blockName: newBlockName, + codeView: false, + currentKeyCode: 38 + }); + } + }); + break; + default: + return; + } + + setTimeout(() => { + if (keyCode === 37 || keyCode === 38 || keyCode === 39 || keyCode === 40) { + this.setState({ currentKeyCode: null }) + } + }, 200); + } + changeMode() { this.setState({ darkMode: !this.state.darkMode }) } handleContentDidMount() { + const iframe = document.querySelector('iframe'); + iframe.contentWindow.document.addEventListener('keydown', this.keyboardNavigation); + iframe.contentWindow.document.addEventListener('click', () => this.setState({ sidebar: false })); + setTimeout(() => { this.setState({ ready: true, @@ -187,7 +252,6 @@ class App extends Component { blockType, blockName, codeView: false }); - } changeTheme(e) { @@ -209,7 +273,7 @@ class App extends Component { themeListRenderer() { const { theme } = this.state; return themeList.map((t, k) => - + ) } @@ -219,7 +283,7 @@ class App extends Component {
Air plant banjo lyft occupy retro adaptogen indego @@ -107,7 +107,7 @@ function DarkFooterA(props) {
- © 2020 madde —
+ © 2020 tailblocks —
@knyttneve
diff --git a/src/blocks/footer/dark/b.js b/src/blocks/footer/dark/b.js
index 6d0977c..f452389 100644
--- a/src/blocks/footer/dark/b.js
+++ b/src/blocks/footer/dark/b.js
@@ -19,7 +19,7 @@ function DarkFooterB(props) {
>
Air plant banjo lyft occupy retro adaptogen indego @@ -107,7 +107,7 @@ function DarkFooterB(props) {
- © 2020 madde — + © 2020 tailblocks — @knyttneve diff --git a/src/blocks/footer/dark/c.js b/src/blocks/footer/dark/c.js index 7df324e..8443f93 100644 --- a/src/blocks/footer/dark/c.js +++ b/src/blocks/footer/dark/c.js @@ -200,7 +200,7 @@ function DarkFooterC(props) {
- © 2020 madde —
+ © 2020 tailblocks —
@knyttneve
diff --git a/src/blocks/footer/dark/d.js b/src/blocks/footer/dark/d.js
index e0c021f..d4ad4b9 100644
--- a/src/blocks/footer/dark/d.js
+++ b/src/blocks/footer/dark/d.js
@@ -18,10 +18,10 @@ function DarkFooterD(props) {
>
- © 2020 madde —
+ © 2020 tailblocks —
@knyttneve
diff --git a/src/blocks/footer/dark/e.js b/src/blocks/footer/dark/e.js
index 8331f83..fecfffd 100644
--- a/src/blocks/footer/dark/e.js
+++ b/src/blocks/footer/dark/e.js
@@ -99,10 +99,10 @@ function DarkFooterE(props) {
>
- © 2020 madde —
+ © 2020 tailblocks —
@knyttneve
diff --git a/src/blocks/footer/light/a.js b/src/blocks/footer/light/a.js
index beb6fd7..05ca12b 100644
--- a/src/blocks/footer/light/a.js
+++ b/src/blocks/footer/light/a.js
@@ -19,7 +19,7 @@ function LightFooterA(props) {
>
Air plant banjo lyft occupy retro adaptogen indego @@ -107,7 +107,7 @@ function LightFooterA(props) {
- © 2020 madde —
+ © 2020 tailblocks —
@knyttneve
diff --git a/src/blocks/footer/light/b.js b/src/blocks/footer/light/b.js
index fee487e..173cff6 100644
--- a/src/blocks/footer/light/b.js
+++ b/src/blocks/footer/light/b.js
@@ -19,7 +19,7 @@ function LightFooterB(props) {
>
Air plant banjo lyft occupy retro adaptogen indego @@ -107,7 +107,7 @@ function LightFooterB(props) {
- © 2020 madde — + © 2020 tailblocks — @knyttneve diff --git a/src/blocks/footer/light/c.js b/src/blocks/footer/light/c.js index 316bdf3..21942b2 100644 --- a/src/blocks/footer/light/c.js +++ b/src/blocks/footer/light/c.js @@ -200,7 +200,7 @@ function LightFooterC(props) {
- © 2020 madde —
+ © 2020 tailblocks —
@knyttneve
diff --git a/src/blocks/footer/light/d.js b/src/blocks/footer/light/d.js
index 233acbb..705e14b 100644
--- a/src/blocks/footer/light/d.js
+++ b/src/blocks/footer/light/d.js
@@ -18,10 +18,10 @@ function LightFooterD(props) {
>
- © 2020 madde —
+ © 2020 tailblocks —
@knyttneve
diff --git a/src/blocks/footer/light/e.js b/src/blocks/footer/light/e.js
index b28edcb..22cdc24 100644
--- a/src/blocks/footer/light/e.js
+++ b/src/blocks/footer/light/e.js
@@ -99,10 +99,10 @@ function LightFooterE(props) {
>
- © 2020 madde —
+ © 2020 tailblocks —
@knyttneve
diff --git a/src/blocks/header/dark/a.js b/src/blocks/header/dark/a.js
index 1ad2a6d..2f85d0c 100644
--- a/src/blocks/header/dark/a.js
+++ b/src/blocks/header/dark/a.js
@@ -18,7 +18,7 @@ function DarkHeaderA(props) {
>