Skip to content

Commit

Permalink
links to kernel code +
Browse files Browse the repository at this point in the history
  • Loading branch information
lue-bird committed Nov 23, 2023
1 parent b70ee3d commit fb660d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions runner/browser/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import {
SetViewportOfOptions,
} from "./index.js";

// Relevant Elm Kernel code: https://github.com/elm/browser/blob/master/src/Elm/Kernel/Browser.js#L322-L328
// Note: `focus` is called using `Elm.Kernel.Browser.call "focus"`
export function focus(id: string): void | DomError {
return withDomNode(id, (el) => el.focus());
}

// Relevant Elm Kernel code: https://github.com/elm/browser/blob/master/src/Elm/Kernel/Browser.js#L322-L328
// Note: `blur` is called using `Elm.Kernel.Browser.call "blur"`
export function blur(id: string): void | DomError {
return withDomNode(id, (el) => el.blur());
}

// Equivalent Elm Kernel code: https://github.com/elm/browser/blob/master/src/Elm/Kernel/Browser.js#L335-L346
export function getViewport(): Viewport {
return {
scene: getBrowserScene(),
Expand All @@ -26,6 +31,7 @@ export function getViewport(): Viewport {
};
}

// Equivalent Elm Kernel code: https://github.com/elm/browser/blob/master/src/Elm/Kernel/Browser.js#L372-L389
export function getViewportOf(id: string): Viewport | DomError {
return withDomNode(id, (el) => ({
scene: {
Expand All @@ -41,17 +47,20 @@ export function getViewportOf(id: string): Viewport | DomError {
}));
}

// Equivalent Elm Kernel code: https://github.com/elm/browser/blob/master/src/Elm/Kernel/Browser.js#L358-L365
export function setViewport(options: SetViewportOptions): void {
window.scroll(options.y, options.y);
}

// Equivalent Elm Kernel code: https://github.com/elm/browser/blob/master/src/Elm/Kernel/Browser.js#L392-L400
export function setViewportOf(options: SetViewportOfOptions): void | DomError {
return withDomNode(options.id, (el) => {
el.scrollLeft = options.x;
el.scrollTop = options.y;
});
}

// Equivalent Elm Kernel code: https://github.com/elm/browser/blob/master/src/Elm/Kernel/Browser.js#L407-L430
export function getElement(id: string): DomElement | DomError {
return withDomNode(id, (el) => {
const rect = el.getBoundingClientRect();
Expand All @@ -77,6 +86,7 @@ export function getElement(id: string): DomElement | DomError {

// Helpers

// Equivalent Elm Kernel code: https://github.com/elm/browser/blob/master/src/Elm/Kernel/Browser.js#L293-L305
function withDomNode<a>(
id: string,
callback: (el: HTMLElement) => a
Expand All @@ -88,6 +98,7 @@ function withDomNode<a>(
return { error: null };
}

// Equivalent Elm Kernel code: https://github.com/elm/browser/blob/master/src/Elm/Kernel/Browser.js#L348-L356
function getBrowserScene(): { width: number; height: number } {
const body = document.body;
const elem = document.documentElement;
Expand Down
2 changes: 2 additions & 0 deletions runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function start(ports: ElmPorts, appElement: HTMLElement) {
{
on: event => event?.addRequestTimezoneOffset,
run: (_config, sendToElm) => {
// // Equivalent Elm Kernel code: https://github.com/elm/time/blob/1.0.0/src/Elm/Kernel/Time.js#L38-L52
sendToElm(-new Date().getTimezoneOffset())
}
},
Expand Down Expand Up @@ -107,6 +108,7 @@ export function start(ports: ElmPorts, appElement: HTMLElement) {
}
}

// Equivalent Elm Kernel code: https://github.com/elm/time/blob/1.0.0/src/Elm/Kernel/Time.js#L27-L35
function getTimezoneName(): string | number {
try {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
Expand Down

0 comments on commit fb660d7

Please sign in to comment.