anki/ts/lib/events.ts

28 lines
1.1 KiB
TypeScript
Raw Normal View History

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
export type EventTargetToMap<A extends EventTarget> = A extends HTMLElement ? HTMLElementEventMap
: A extends Document ? DocumentEventMap
: A extends Window ? WindowEventMap
: A extends FileReader ? FileReaderEventMap
: A extends Element ? ElementEventMap
: A extends Animation ? AnimationEventMap
: A extends EventSource ? EventSourceEventMap
: A extends AbortSignal ? AbortSignalEventMap
: A extends AbstractWorker ? AbstractWorkerEventMap
Reverse-engineer surrounding with execCommand (#1377) * Add utility functions for saving and restoring the caret location Implement surroundNoSplitting Clarify surroundNoSplitting comments Start implementing surroundSplitting and triggerIfSimpleInput Fix after rebase Implement findBefore / findAfter in lib/surround * to merge adjacent nodes into the surrounding nodes Use new prettier settings with lib/{location,surround} Fix imports that I missed to rename Add some tests for find-adjacent Split find-within from find-adjacent Normalize nodes after insertion in surroundNoSplitting Do not deep clone surroundNode -> no intention of supporting deep nodes, as normalization would be impossible Add some tests concerning nested surrounding nested nodes Select surroundedRange after surrounding Fix ascendWhileSingleInline A flawed first surround/trigger implementation Move trigger out of lib/surround Implement Input Manager as a way to handle bold on empty selection Switch bold button away from execCommand Pass in Matcher instead of selector to find-adjacent and surroundNoSplitting * Also adds a failing test for no-splitting Refactor find-adjacent * add failing test when findBefore's nodes have different amounts of child nodes Change type signature of find-adjacent methods to more single-concern Add test for surrounding where adjacent block becomes three Text elements Make nodes found within surrounded range extend the ranges endOffset Add base parameter to surroundNoSplitting to stop ascending beyond container Stop surrounding from bubbling beyond base in merge-match Make all tests pass Add some failing tests to point to future development Add empty elements as constant Implement a broken version of unsurround Even split text if it creates zero-length texts -> they are still valid, despite what Chromium says Rename {start,end} to {start,end}Container Add more unit tests with surround after a nested element Set endOffset after split-off possibly zero length text nodes Deal with empty elements when surrounding Only include split off end text if zero length Use range anchors instead off calcluating surroundedRange from offsets * this approach allows for removal of base elements when unsurrounding Comment out test which fail because of jsdom bugs We'll be able to enable them again after Jest 28 Make the first unsurround tests pass Add new failing test for unsurround text within tag Fix unsurround Test is deactivated until Jest 28 Rewrite input-manager and trigger callback after insertion Avoid creating zero length text nodes by using insertBefore when appropriate Implement matches vs keepMatches Make shadow root and editable element available on component tree Make WithState work with asynchronous updater functions Add new Bold/Italic/UnderlineButton using our logic Add failing test for unsurrounding * Move surround/ to domlib * Add jest dependency * Make find-within return a sum type array rather than two arrays * Use FoundMatch sum-type for find-above (and find-within) * Fix issue where elements could be cleared twice * if they are IN the range.endContainer * Pass remaining test * Add another failing test * Fix empty text nodes being considered for surrounding * Satisfy svelte check * Make on more type correct * Satisfy remaining tests * Add missing copyright header
2021-11-18 10:18:39 +01:00
: never;
export function on<T extends EventTarget, K extends keyof EventTargetToMap<T>>(
target: T,
Reverse-engineer surrounding with execCommand (#1377) * Add utility functions for saving and restoring the caret location Implement surroundNoSplitting Clarify surroundNoSplitting comments Start implementing surroundSplitting and triggerIfSimpleInput Fix after rebase Implement findBefore / findAfter in lib/surround * to merge adjacent nodes into the surrounding nodes Use new prettier settings with lib/{location,surround} Fix imports that I missed to rename Add some tests for find-adjacent Split find-within from find-adjacent Normalize nodes after insertion in surroundNoSplitting Do not deep clone surroundNode -> no intention of supporting deep nodes, as normalization would be impossible Add some tests concerning nested surrounding nested nodes Select surroundedRange after surrounding Fix ascendWhileSingleInline A flawed first surround/trigger implementation Move trigger out of lib/surround Implement Input Manager as a way to handle bold on empty selection Switch bold button away from execCommand Pass in Matcher instead of selector to find-adjacent and surroundNoSplitting * Also adds a failing test for no-splitting Refactor find-adjacent * add failing test when findBefore's nodes have different amounts of child nodes Change type signature of find-adjacent methods to more single-concern Add test for surrounding where adjacent block becomes three Text elements Make nodes found within surrounded range extend the ranges endOffset Add base parameter to surroundNoSplitting to stop ascending beyond container Stop surrounding from bubbling beyond base in merge-match Make all tests pass Add some failing tests to point to future development Add empty elements as constant Implement a broken version of unsurround Even split text if it creates zero-length texts -> they are still valid, despite what Chromium says Rename {start,end} to {start,end}Container Add more unit tests with surround after a nested element Set endOffset after split-off possibly zero length text nodes Deal with empty elements when surrounding Only include split off end text if zero length Use range anchors instead off calcluating surroundedRange from offsets * this approach allows for removal of base elements when unsurrounding Comment out test which fail because of jsdom bugs We'll be able to enable them again after Jest 28 Make the first unsurround tests pass Add new failing test for unsurround text within tag Fix unsurround Test is deactivated until Jest 28 Rewrite input-manager and trigger callback after insertion Avoid creating zero length text nodes by using insertBefore when appropriate Implement matches vs keepMatches Make shadow root and editable element available on component tree Make WithState work with asynchronous updater functions Add new Bold/Italic/UnderlineButton using our logic Add failing test for unsurrounding * Move surround/ to domlib * Add jest dependency * Make find-within return a sum type array rather than two arrays * Use FoundMatch sum-type for find-above (and find-within) * Fix issue where elements could be cleared twice * if they are IN the range.endContainer * Pass remaining test * Add another failing test * Fix empty text nodes being considered for surrounding * Satisfy svelte check * Make on more type correct * Satisfy remaining tests * Add missing copyright header
2021-11-18 10:18:39 +01:00
eventType: Exclude<K, symbol | number>,
handler: (this: T, event: EventTargetToMap<T>[K]) => void,
options?: AddEventListenerOptions,
): () => void {
Reverse-engineer surrounding with execCommand (#1377) * Add utility functions for saving and restoring the caret location Implement surroundNoSplitting Clarify surroundNoSplitting comments Start implementing surroundSplitting and triggerIfSimpleInput Fix after rebase Implement findBefore / findAfter in lib/surround * to merge adjacent nodes into the surrounding nodes Use new prettier settings with lib/{location,surround} Fix imports that I missed to rename Add some tests for find-adjacent Split find-within from find-adjacent Normalize nodes after insertion in surroundNoSplitting Do not deep clone surroundNode -> no intention of supporting deep nodes, as normalization would be impossible Add some tests concerning nested surrounding nested nodes Select surroundedRange after surrounding Fix ascendWhileSingleInline A flawed first surround/trigger implementation Move trigger out of lib/surround Implement Input Manager as a way to handle bold on empty selection Switch bold button away from execCommand Pass in Matcher instead of selector to find-adjacent and surroundNoSplitting * Also adds a failing test for no-splitting Refactor find-adjacent * add failing test when findBefore's nodes have different amounts of child nodes Change type signature of find-adjacent methods to more single-concern Add test for surrounding where adjacent block becomes three Text elements Make nodes found within surrounded range extend the ranges endOffset Add base parameter to surroundNoSplitting to stop ascending beyond container Stop surrounding from bubbling beyond base in merge-match Make all tests pass Add some failing tests to point to future development Add empty elements as constant Implement a broken version of unsurround Even split text if it creates zero-length texts -> they are still valid, despite what Chromium says Rename {start,end} to {start,end}Container Add more unit tests with surround after a nested element Set endOffset after split-off possibly zero length text nodes Deal with empty elements when surrounding Only include split off end text if zero length Use range anchors instead off calcluating surroundedRange from offsets * this approach allows for removal of base elements when unsurrounding Comment out test which fail because of jsdom bugs We'll be able to enable them again after Jest 28 Make the first unsurround tests pass Add new failing test for unsurround text within tag Fix unsurround Test is deactivated until Jest 28 Rewrite input-manager and trigger callback after insertion Avoid creating zero length text nodes by using insertBefore when appropriate Implement matches vs keepMatches Make shadow root and editable element available on component tree Make WithState work with asynchronous updater functions Add new Bold/Italic/UnderlineButton using our logic Add failing test for unsurrounding * Move surround/ to domlib * Add jest dependency * Make find-within return a sum type array rather than two arrays * Use FoundMatch sum-type for find-above (and find-within) * Fix issue where elements could be cleared twice * if they are IN the range.endContainer * Pass remaining test * Add another failing test * Fix empty text nodes being considered for surrounding * Satisfy svelte check * Make on more type correct * Satisfy remaining tests * Add missing copyright header
2021-11-18 10:18:39 +01:00
target.addEventListener(eventType, handler as EventListener, options);
return () => target.removeEventListener(eventType, handler as EventListener, options);
}
export function preventDefault(event: Event): void {
event.preventDefault();
}