forked from riccardoperra/codeimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.ts
More file actions
60 lines (49 loc) · 1.73 KB
/
editor.ts
File metadata and controls
60 lines (49 loc) · 1.73 KB
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
import {createStore, select, withProps} from '@ngneat/elf';
import {localStorageStrategy, persistState} from '@ngneat/elf-persist-state';
import {distinctUntilChanged, map} from 'rxjs';
import {appEnvironment} from '../core/configuration';
import {SUPPORTED_FONTS} from '../core/configuration/font';
import shallow from '../core/helpers/shallow';
import {elfAutoSettersFactory} from '../core/store/elf-auto-setters-factory';
interface EditorState {
languageId: string;
themeId: string;
code: string;
fontId: string;
fontWeight: number;
showLineNumbers: boolean;
focused: boolean;
}
const initialState: EditorState = {
code: appEnvironment.defaultState.editor.code,
languageId: appEnvironment.defaultState.editor.languageId,
themeId: appEnvironment.defaultState.editor.theme.id,
showLineNumbers: false,
fontId: appEnvironment.defaultState.editor.font.id,
fontWeight: appEnvironment.defaultState.editor.font.types[0].weight,
focused: false,
};
const store = createStore(
{name: 'editor'},
withProps<EditorState>(initialState),
);
export const updateEditorStore = store.update.bind(store);
persistState(store, {storage: localStorageStrategy, key: '@store/editor'});
export const {
setFocused: setFocus,
setFontId,
setFontWeight,
setLanguageId,
setShowLineNumbers,
setThemeId: setTheme,
setCode,
} = elfAutoSettersFactory(store);
export const editor$ = store.pipe(distinctUntilChanged(shallow));
const fontId$ = editor$.pipe(select(store => store.fontId));
export const editorLanguageId$ = editor$.pipe(
select(store => store.languageId),
);
export const font$ = fontId$.pipe(
map(id => SUPPORTED_FONTS.find(font => font.id === id)),
);
export const focusedEditor$ = editor$.pipe(select(store => store.focused));