-
Notifications
You must be signed in to change notification settings - Fork 0
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
Vim Mode 2 - Various helpers I use to implement Vim Mode #1
Conversation
There was a problem hiding this 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 :)
src/ts/core/common/assert.ts
Outdated
@@ -0,0 +1,7 @@ | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👀
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
src/ts/core/common/keyboard.ts
Outdated
@@ -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.
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spooky
src/ts/scripts/dom.ts
Outdated
@@ -0,0 +1,11 @@ | |||
export const injectStyle = (css: string, tagId: string) => { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
src/ts/scripts/mutation-observer.ts
Outdated
@@ -0,0 +1,42 @@ | |||
import {assumeExists} from 'SRC/core/common/assert' |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
src/ts/scripts/mutation-observer.ts
Outdated
return () => waitForLoad.disconnect() | ||
} | ||
|
||
export const waitForSelectorToExist = (selector: string, observeInside = document.body) => { |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok :)
src/ts/core/roam/roam-block.ts
Outdated
return navigator.clipboard.writeText(`{{embed: ((${getBlockUid(blockId)}))}}`) | ||
} | ||
|
||
export const copyBlockReference = (blockId: string | undefined) => { |
There was a problem hiding this comment.
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?
src/ts/core/roam/roam.ts
Outdated
const foldButton = assumeExists( | ||
assumeExists(block.parentElement).querySelector(Selectors.foldButton) | ||
); | ||
await Mouse.hover(foldButton as HTMLElement); |
There was a problem hiding this comment.
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
src/ts/core/common/object.ts
Outdated
@@ -0,0 +1,37 @@ | |||
export const invertObject = <K, V>(obj: { |
There was a problem hiding this comment.
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
src/ts/core/common/object.ts
Outdated
[key: string]: V, | ||
} | ||
|
||
export const mapObjectValues = <X, Y>(obj: ObjectMap<X>, mapFn: (x: X) => Y): ObjectMap<Y> => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
src/ts/scripts/mutation-observer.ts
Outdated
handleChange() | ||
}) | ||
|
||
waitForLoad.observe(observeInside || assumeExists(document.querySelector(selector)), { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
5d12c6e
to
4a765f0
Compare
8ce0c2b
to
c695d4c
Compare
8b9e383
to
988e7df
Compare
988e7df
to
6e43e9e
Compare
…her than introducing a seperate method for that
8d89a2a
to
309d6c1
Compare
There was a problem hiding this 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 :)
src/ts/core/roam/roam.ts
Outdated
@@ -19,7 +22,7 @@ export const Roam = { | |||
|
|||
getRoamBlockInput(): HTMLTextAreaElement | null { | |||
const element = getActiveEditElement() | |||
if (element.tagName.toLocaleLowerCase() !== 'textarea') { | |||
if (!element || element.tagName.toLocaleLowerCase() !== 'textarea') { |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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} = { |
There was a problem hiding this comment.
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 =\
…I still use the `observeInside` option of `waitForSelectorToExist` later
|
Oh dang, I'll investigate |
Hmm I think I need to update the import paths in the existing tests, after allowing absolute imports in jest |
Opened a new PR with the fix: roam-unofficial#94 |
thanks! |
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.