Skip to content

Commit 1f974da

Browse files
authored
Merge pull request #7 from tomoam/update-up-to-20221223
2022/12/23 迄の更新に追従
2 parents aa5a3e3 + e3d56e6 commit 1f974da

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

content/tutorial/03-advanced-svelte/04-actions/02-adding-parameters-to-actions/app-a/src/lib/longpress.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function longpress(node, duration) {
1616

1717
return {
1818
destroy() {
19+
clearTimeout(timer);
1920
node.removeEventListener('mousedown', handleMousedown);
2021
node.removeEventListener('mouseup', handleMouseup);
2122
}

content/tutorial/03-advanced-svelte/04-actions/02-adding-parameters-to-actions/app-b/src/lib/longpress.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export function longpress(node, duration) {
1919
duration = newDuration;
2020
},
2121
destroy() {
22+
clearTimeout(timer);
2223
node.removeEventListener('mousedown', handleMousedown);
2324
node.removeEventListener('mouseup', handleMouseup);
2425
}

content/tutorial/common/src/__client.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,18 @@ if (import.meta.hot) {
8989
);
9090
});
9191
}
92+
93+
/**
94+
* The iframe sometimes takes focus control in ways we can't prevent
95+
* while the editor is focussed. Refocus the editor in these cases.
96+
*/
97+
window.addEventListener('focusin', (e) => {
98+
if (e.target.tagName === 'BODY') {
99+
parent.postMessage(
100+
{
101+
type: 'focus_on_editor'
102+
},
103+
'*'
104+
);
105+
}
106+
});

src/routes/tutorial/[slug]/Editor.svelte

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
* This boolean tracks whether or not the editor should be refocused.
4242
*/
4343
let preserve_focus = true;
44+
/** @type {any} */
45+
let remove_focus_timeout;
4446
4547
onMount(() => {
4648
let destroyed = false;
@@ -241,34 +243,27 @@
241243
</script>
242244
243245
<svelte:window
244-
on:pointerdown={(e) => {
245-
if (!container.contains(/** @type {HTMLElement} */ (e.target))) {
246-
preserve_focus = false;
246+
on:message={(e) => {
247+
if (preserve_focus && e.data.type === 'focus_on_editor') {
248+
instance?.editor.focus();
247249
}
248250
}}
249251
/>
250252
251253
<div bind:clientWidth={w} bind:clientHeight={h}>
252254
<div
253255
bind:this={container}
254-
on:keydown={(e) => {
255-
if (e.key === 'Tab') {
256-
preserve_focus = false;
257-
258-
setTimeout(() => {
259-
preserve_focus = true;
260-
}, 0);
261-
}
262-
}}
263256
on:focusin={() => {
257+
clearTimeout(remove_focus_timeout);
264258
preserve_focus = true;
265259
}}
266260
on:focusout={() => {
267-
if (preserve_focus) {
268-
setTimeout(() => {
269-
instance?.editor.focus();
270-
}, 0);
271-
}
261+
// Heuristic: user did refocus themmselves if focus_on_editor
262+
// doesn't happen in the next few miliseconds. Needed
263+
// because else navigations inside the iframe refocus the editor.
264+
remove_focus_timeout = setTimeout(() => {
265+
preserve_focus = false;
266+
}, 500)
272267
}}
273268
/>
274269
</div>

0 commit comments

Comments
 (0)