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

Add eslint #10

Merged
merged 3 commits into from
Jan 4, 2024
Merged
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
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
Loading