Skip to content

Commit

Permalink
Add eslint (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahangarha authored Jan 4, 2024
1 parent b192711 commit 3102103
Show file tree
Hide file tree
Showing 6 changed files with 3,902 additions and 181 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
.bscode/
.parcel-cache/
.github/

dist/
13 changes: 13 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": "airbnb-base",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
}
}
22 changes: 22 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Linting

on:
push:
branches:
- 'main'
pull_request:
branches:
- 'main'

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: '18.x'
- name: Run eslint
run: npm i && npm run lint
117 changes: 61 additions & 56 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import '@logseq/libs'
import '@logseq/libs';

const graphDocument = window.parent.document
const graphDocument = window.parent.document;

const setDirAuto = (node) => node.setAttribute('dir', 'auto')
const setDirAutoToAll = (nodes) => nodes.forEach((node) => setDirAuto(node))
const setDirAuto = (node) => node.setAttribute('dir', 'auto');
const setDirAutoToAll = (nodes) => nodes.forEach((node) => setDirAuto(node));

const handleLeftRightKey = (e) => {
if (e.shiftKey || e.ctrlKey) return
if (!["ArrowRight", "ArrowLeft"].includes(e.code)) return
if (e.shiftKey || e.ctrlKey) return;
if (!['ArrowRight', 'ArrowLeft'].includes(e.code)) return;

const activeElement = graphDocument.activeElement
const activeElementEffectiveDirection = window.getComputedStyle(activeElement).direction
const { activeElement } = graphDocument;
const activeElementEffectiveDirection = window.getComputedStyle(activeElement).direction;

if (activeElementEffectiveDirection === 'ltr') return
if (activeElementEffectiveDirection === 'ltr') return;

const commonKeyboardEventProperties = {
bubbles: false,
Expand All @@ -21,65 +21,30 @@ const handleLeftRightKey = (e) => {
ctrlKey: false,
altKey: false,
metaKey: false,
}
};

const rightKeyCode = {
key: "ArrowRight",
key: 'ArrowRight',
keyCode: 39,
}
};

const leftKeyCode = {
key: "ArrowLeft",
key: 'ArrowLeft',
keyCode: 37,
}
};

const swipedKeyCode = e.code == "ArrowRight" ? leftKeyCode : rightKeyCode;
const swipedKeyCode = e.code === 'ArrowRight' ? leftKeyCode : rightKeyCode;

const keyboardEvent = new KeyboardEvent("keydown", {
const keyboardEvent = new KeyboardEvent('keydown', {
...swipedKeyCode,
...commonKeyboardEventProperties,
})
});

// eslint-disable-next-line no-restricted-globals
top?.dispatchEvent(keyboardEvent);
// eslint-disable-next-line no-restricted-globals
top?.dispatchEvent(keyboardEvent);
}

const applyBidi = () => {
const cssSelector = 'div.ls-block:not([dir]), div.ls-page-title:not([dir])'

// initial run on whole document
setDirAutoToAll(graphDocument.querySelectorAll(cssSelector))

// Define the callback function
const processBlocks = (mutationsList, observer) => {
for(let mutation of mutationsList) {

if (mutation.type !== 'childList') continue;

for(let addedNode of mutation.addedNodes) {
if (addedNode.classList?.contains('ls-block')) setDirAuto(addedNode)

const subLsBlocks = addedNode.querySelectorAll(cssSelector)
setDirAutoToAll(subLsBlocks)
}
}
};

const observer = new MutationObserver(processBlocks);
observer.observe(graphDocument, { childList: true, subtree: true });
}

const applyCustomBidiStyle = () => {
logseq.provideStyle(customBidiStyle)
}

const main = (e) => {
applyBidi()
applyCustomBidiStyle()
top?.addEventListener("keydown", handleLeftRightKey);
}

logseq.ready(main).catch(console.error)
};

const customBidiStyle = `
.pr-2 {
Expand Down Expand Up @@ -296,4 +261,44 @@ blockquote {
float: inline-end;
}
}
`
`;

const applyBidi = () => {
const cssSelector = 'div.ls-block:not([dir]), div.ls-page-title:not([dir])';

// initial run on whole document
setDirAutoToAll(graphDocument.querySelectorAll(cssSelector));

// Define the callback function
const processBlocks = (mutationsList) => {
mutationsList.forEach((mutation) => {
if (mutation.type !== 'childList') return;

mutation.addedNodes.forEach((addedNode) => {
if (addedNode.classList?.contains('ls-block')) setDirAuto(addedNode);

const subLsBlocks = addedNode.querySelectorAll(cssSelector);
setDirAutoToAll(subLsBlocks);
});
});
};

const observer = new MutationObserver(processBlocks);
observer.observe(graphDocument, { childList: true, subtree: true });
};

const applyCustomBidiStyle = () => {
// eslint-disable-next-line no-undef
logseq.provideStyle(customBidiStyle);
};

const main = () => {
applyBidi();
applyCustomBidiStyle();

// eslint-disable-next-line no-restricted-globals
top?.addEventListener('keydown', handleLeftRightKey);
};

// eslint-disable-next-line no-undef, no-console
logseq.ready(main).catch(console.error);
Loading

0 comments on commit 3102103

Please sign in to comment.