aboutsummaryrefslogtreecommitdiff
path: root/src/console/actions/console.ts
blob: bce2c67ef14dbdf6a9d97390394a89935ce3f014 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import * as messages from "../../shared/messages";

export const CONSOLE_SHOW_COMMAND = "console.show.command";
export const CONSOLE_SHOW_ERROR = "console.show.error";
export const CONSOLE_SHOW_INFO = "console.show.info";
export const CONSOLE_HIDE_COMMAND = "console.hide.command";
export const CONSOLE_SET_CONSOLE_TEXT = "console.set.command";
export const CONSOLE_SHOW_FIND = "console.show.find";
export const CONSOLE_HIDE = "console.hide";

export interface HideAction {
  type: typeof CONSOLE_HIDE;
}

export interface ShowCommand {
  type: typeof CONSOLE_SHOW_COMMAND;
  text: string;
}

export interface ShowFindAction {
  type: typeof CONSOLE_SHOW_FIND;
}

export interface ShowErrorAction {
  type: typeof CONSOLE_SHOW_ERROR;
  text: string;
}

export interface ShowInfoAction {
  type: typeof CONSOLE_SHOW_INFO;
  text: string;
}

export interface HideCommandAction {
  type: typeof CONSOLE_HIDE_COMMAND;
}

export interface SetConsoleTextAction {
  type: typeof CONSOLE_SET_CONSOLE_TEXT;
  consoleText: string;
}

export type ConsoleAction =
  | HideAction
  | ShowCommand
  | ShowFindAction
  | ShowErrorAction
  | ShowInfoAction
  | HideCommandAction
  | SetConsoleTextAction;

const hide = (): ConsoleAction => {
  return {
    type: CONSOLE_HIDE,
  };
};

const showCommand = (text: string): ShowCommand => {
  return {
    type: CONSOLE_SHOW_COMMAND,
    text,
  };
};

const showFind = (): ShowFindAction => {
  return {
    type: CONSOLE_SHOW_FIND,
  };
};

const showError = (text: string): ShowErrorAction => {
  return {
    type: CONSOLE_SHOW_ERROR,
    text: text,
  };
};

const showInfo = (text: string): ShowInfoAction => {
  return {
    type: CONSOLE_SHOW_INFO,
    text: text,
  };
};

const hideCommand = (): HideCommandAction => {
  window.top.postMessage(
    JSON.stringify({
      type: messages.CONSOLE_UNFOCUS,
    }),
    "*"
  );
  return {
    type: CONSOLE_HIDE_COMMAND,
  };
};

const enterCommand = async (text: string): Promise<HideCommandAction> => {
  await browser.runtime.sendMessage({
    type: messages.CONSOLE_ENTER_COMMAND,
    text,
  });
  return hideCommand();
};

const enterFind = (text?: string): HideCommandAction => {
  window.top.postMessage(
    JSON.stringify({
      type: messages.CONSOLE_ENTER_FIND,
      text,
    }),
    "*"
  );
  return hideCommand();
};

const setConsoleText = (consoleText: string): SetConsoleTextAction => {
  return {
    type: CONSOLE_SET_CONSOLE_TEXT,
    consoleText,
  };
};

export {
  hide,
  showCommand,
  showFind,
  showError,
  showInfo,
  hideCommand,
  setConsoleText,
  enterCommand,
  enterFind,
};