This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support copy / cut / paste via application menu
EventEmitterを使わないと、「今何が選択されているか」をEditorStoreに突っ込んでおかないと アプリケーションメニューのコールバック側から知ることができないが そのように対応すると結構大掛かりな仕掛けが必要になるので EventEmitterからイベントを投げて、その時フォーカスされてる要素に処理を移譲するようにした
- Loading branch information
Showing
10 changed files
with
129 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export class EventEmitter<T extends object> { | ||
protected exclusiveEvents: string[] = [] | ||
private listeners: { [K in keyof T]: ((arg: T[K]) => void)[] } = Object.create({}) | ||
|
||
public on<K extends keyof T>(event: K, listener: (arg: T[K]) => void) { | ||
const listeners = this.listeners[event] = (this.listeners[event] || []) | ||
|
||
if (this.exclusiveEvents.includes(event)) { | ||
this.listeners[event] = [listener] | ||
} else { | ||
listeners.push(listener) | ||
} | ||
} | ||
|
||
public off<K extends keyof T>(event: K, listener: (arg: T[K]) => void) { | ||
if (!this.listeners[event]) return | ||
|
||
const index = this.listeners[event].findIndex(l => l === listener) | ||
if (index === -1) return | ||
this.listeners[event].splice(index, 1) | ||
} | ||
|
||
public emit<K extends keyof T>(event: K, arg: T[K]) { | ||
if (!this.listeners[event]) return | ||
this.listeners[event].forEach(listener => listener(arg)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { EventEmitter } from '../../utils/EventEmitter' | ||
|
||
export enum GlobalEvent { | ||
copyViaApplicationMenu = 'copy', | ||
cutViaApplicationMenu = 'cut', | ||
pasteViaApplicationMenu = 'paste', | ||
} | ||
|
||
interface Events { | ||
[GlobalEvent.copyViaApplicationMenu]: {} | ||
[GlobalEvent.cutViaApplicationMenu]: {} | ||
[GlobalEvent.pasteViaApplicationMenu]: {} | ||
} | ||
|
||
export const GlobalEvents = new class extends EventEmitter<Events> { | ||
protected exclusiveEvents = [ | ||
GlobalEvent.copyViaApplicationMenu, | ||
GlobalEvent.cutViaApplicationMenu, | ||
GlobalEvent.pasteViaApplicationMenu, | ||
] | ||
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters