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

Vim Mode 2 - Various helpers I use to implement Vim Mode #1

Merged

Conversation

tntmarket
Copy link
Owner

This is part of a larger change to support vim style navigation (roam-unofficial#63)

This includes utilities to work with lists and objects and wrappers around browser APIs.

Copy link

@Stvad Stvad left a comment

Choose a reason for hiding this comment

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

a lot of these functions are amenable to and would benefit from testing :)

@@ -0,0 +1,7 @@

Copy link

Choose a reason for hiding this comment

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

👀

Copy link
Owner Author

@tntmarket tntmarket Jun 25, 2020

Choose a reason for hiding this comment

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

Would you prefer I use foo!.bar instead? (I recently learned that Typescript has this feature).

The explicit asserts sometimes save me time when debugging, but I recognize they clutter the code.

Copy link

Choose a reason for hiding this comment

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

this was just about newline 😛. I believe !. does not change the runtime behavior so your assert is actually different, I'm totally fine to keep it :)

@@ -9,7 +9,7 @@ export const Keyboard = {
BASE_DELAY: 20,

async simulateKey(code: number, delayOverride: number = 0, opts?: KeyboardEventInit) {
;['keydown', 'keyup'].forEach(eventType =>
['keydown', 'keyup'].forEach(eventType =>

This comment was marked as resolved.

Copy link
Owner Author

Choose a reason for hiding this comment

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

spooky

@@ -0,0 +1,11 @@
export const injectStyle = (css: string, tagId: string) => {

This comment was marked as resolved.

@@ -0,0 +1,42 @@
import {assumeExists} from 'SRC/core/common/assert'

This comment was marked as resolved.

return () => waitForLoad.disconnect()
}

export const waitForSelectorToExist = (selector: string, observeInside = document.body) => {
Copy link

Choose a reason for hiding this comment

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

in a parallel PR I recommended using https://github.com/uzairfarooq/arrive - I'm wondering if we should use it here ?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I'll try it out

Copy link
Owner Author

@tntmarket tntmarket Jun 26, 2020

Choose a reason for hiding this comment

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

I tried arrive, and I'm not sure it's worth it.

Observing right panel toggles via arrive looks like this:

import Arrive from 'arrive'

// Stick the variable somewhere, so arrive can apply it's monkey patches
// Otherwise, webpack will shake it away
Arrive

export const RoamEvent = {
    onRightPanelToggle(): {
        document.querySelector(Selectors.rightPanel).arrive(Selectors.sidebarContent, () => {
            console.log('right panel opened')
        })
        document.querySelector(Selectors.rightPanel).leave(Selectors.sidebarContent, () => {
            console.log('right panel closed')
        })
    },
}

Observing page addition/removal from the sidebar looks like this:

document.querySelector(Selectors.sidebarContent).arrive('div', () => {
    console.log('something might've been added to right panel')
})
document.querySelector(Selectors.sidebarContent).leave('div', () => {
    console.log("something might've been removed from right panel")
})

arrive observes the whole subtree, so div arrivals actually trigger many times whenever anything in the right panel changes. I'm paranoid that this may impact performance, or have unintended side effects if someone is just editing blocks inside the sidebar

arrive also doesn't have typescript definitions

Copy link

Choose a reason for hiding this comment

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

ok :)

return navigator.clipboard.writeText(`{{embed: ((${getBlockUid(blockId)}))}}`)
}

export const copyBlockReference = (blockId: string | undefined) => {
Copy link

Choose a reason for hiding this comment

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

name parameter something like htmlBlockId to distinguish from roam block?

const foldButton = assumeExists(
assumeExists(block.parentElement).querySelector(Selectors.foldButton)
);
await Mouse.hover(foldButton as HTMLElement);
Copy link

Choose a reason for hiding this comment

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

move type cast to variable declaration

@@ -0,0 +1,37 @@
export const invertObject = <K, V>(obj: {
Copy link

Choose a reason for hiding this comment

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

another thing we can get from lodash

[key: string]: V,
}

export const mapObjectValues = <X, Y>(obj: ObjectMap<X>, mapFn: (x: X) => Y): ObjectMap<Y> => {
Copy link

Choose a reason for hiding this comment

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

another thing we can get from lodash. 's probably enough justification to take a dependency on it (or similar lib?) by this point

Copy link
Owner Author

Choose a reason for hiding this comment

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

I'll use lodash. I think webpack tree-shakes the methods we don't use, so it should be cheap

handleChange()
})

waitForLoad.observe(observeInside || assumeExists(document.querySelector(selector)), {
Copy link

Choose a reason for hiding this comment

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

should we have separate functions for observing the element and selector?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I'll decouple picking which element to observe from actually observing it

@tntmarket tntmarket force-pushed the vim_mode_1_allow_absolute_imports branch from 5d12c6e to 4a765f0 Compare June 25, 2020 03:47
@tntmarket tntmarket force-pushed the vim_mode_2_various_utilities_to_that_are_used_by_vim_mode branch from 8ce0c2b to c695d4c Compare June 25, 2020 04:58
@tntmarket tntmarket force-pushed the vim_mode_2_various_utilities_to_that_are_used_by_vim_mode branch 2 times, most recently from 8b9e383 to 988e7df Compare June 25, 2020 05:29
@tntmarket tntmarket force-pushed the vim_mode_2_various_utilities_to_that_are_used_by_vim_mode branch from 988e7df to 6e43e9e Compare June 25, 2020 05:41
@tntmarket tntmarket force-pushed the vim_mode_2_various_utilities_to_that_are_used_by_vim_mode branch from 8d89a2a to 309d6c1 Compare June 26, 2020 20:28
Copy link

@Stvad Stvad left a comment

Choose a reason for hiding this comment

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

looks good to me now :)

@@ -19,7 +22,7 @@ export const Roam = {

getRoamBlockInput(): HTMLTextAreaElement | null {
const element = getActiveEditElement()
if (element.tagName.toLocaleLowerCase() !== 'textarea') {
if (!element || element.tagName.toLocaleLowerCase() !== 'textarea') {
Copy link

Choose a reason for hiding this comment

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

nit: use ?.


const observeElement = (
observeInside: HTMLElement,
handleChange: () => void,
Copy link

Choose a reason for hiding this comment

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

wonder if this should accept the changed element.

import {invertObject} from 'SRC/core/common/object'

// 2nd column represents shifted keys
export const KEY_TO_CODE: {[key: string]: number} = {
Copy link

Choose a reason for hiding this comment

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

yeah, the amount of workarounds we need in #2 is very sad =\

@tntmarket tntmarket changed the base branch from vim_mode_1_allow_absolute_imports to master June 27, 2020 22:33
…I still use the `observeInside` option of `waitForSelectorToExist` later
@Stvad
Copy link

Stvad commented Jun 28, 2020

npm run test is failing now :(

@tntmarket
Copy link
Owner Author

Oh dang, I'll investigate

@tntmarket
Copy link
Owner Author

Hmm I think I need to update the import paths in the existing tests, after allowing absolute imports in jest

@tntmarket
Copy link
Owner Author

Opened a new PR with the fix: roam-unofficial#94

@Stvad
Copy link

Stvad commented Jun 28, 2020

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants