-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
Editor.tsx
141 lines (126 loc) · 3.74 KB
/
Editor.tsx
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
134
135
136
137
138
139
140
141
/* eslint-disable consistent-return */
import React from 'react';
// @ts-ignore
import { useChannel, API, useCurrentStory } from '@storybook/api';
import { STORY_CHANGED } from '@storybook/core-events';
import { styled } from '@storybook/theming';
import { SyntaxHighlighter } from '@storybook/components';
import { SourceLoaderEvent, EVENT_NEW_SOURCE } from './constants';
import { useEditor } from './useEditor';
interface CodeLoc {
col: number;
line: number;
}
interface LocationMap {
startLoc: CodeLoc;
endLoc: CodeLoc;
startBody: CodeLoc;
endBody: CodeLoc;
}
interface SourceLoaderInfo {
edition: {
source: string;
prefix: string;
mainFileLocation: string;
dependencies?: Array<string>;
localDependencies?: Record<string, string>;
};
story: {
kind: string;
story: string;
};
location: {
currentLocation: LocationMap;
locationsMap: {
[key: string]: LocationMap;
};
};
}
const EditorWrapper = styled.div`
padding: 10px;
`;
const CodeEditor = styled.textarea`
position: absolute;
margin: 0px;
border: 0;
padding: 0;
font-family: 'Operator Mono', 'Fira Code Retina', 'Fira Code', 'FiraCode-Retina', 'Andale Mono',
'Lucida Console', Consolas, Monaco, monospace;
line-height: 18px;
top: 10px;
left: 12px;
-webkit-text-fill-color: transparent;
z-index: 999;
background: transparent;
height: 97%;
width: 98%;
`;
const Editor = ({ api }: { api: API }) => {
const emit = useChannel({});
const story = useCurrentStory();
const storyId = story && story.id ? story.id : '';
const [initialCode, setInitialCode] = useEditor();
const [metaData, setMetadata] = React.useState<SourceLoaderInfo | null>(null);
let previousSource = '';
const loadStoryCode = (sourceLoader: SourceLoaderInfo) => {
const sourceCode = sourceLoader.edition.source;
setMetadata(sourceLoader);
if (previousSource === sourceCode || initialCode[storyId]) {
return null;
}
previousSource = sourceCode;
const sourceSplitted = sourceCode.split('\n');
if (sourceLoader.location) {
const sourceNormalized = sourceSplitted
.filter((_: any, idx: number) => {
return (
idx >= sourceLoader.location.currentLocation.startLoc.line - 1 &&
idx < sourceLoader.location.currentLocation.endLoc.line
);
})
.join('\n')
.replace('export', '');
setInitialCode(sourceNormalized, storyId);
}
};
useChannel({
[STORY_CHANGED]: nextStory => {
const nextStorySavedCode = initialCode[nextStory];
if (metaData && !nextStorySavedCode) {
const sourceLoaded = metaData.edition.source;
const sourceNormalized = sourceLoaded
.split('\n')
.filter((_: any, idx: number) => {
return (
idx >= metaData.location.locationsMap[nextStory].startLoc.line - 1 &&
idx < metaData.location.locationsMap[nextStory].endLoc.line
);
})
.join('\n')
.replace('export', '');
setInitialCode(sourceNormalized, storyId);
} else if (nextStorySavedCode) {
emit(EVENT_NEW_SOURCE, nextStorySavedCode);
}
},
});
React.useEffect(() => {
if (story && storyId) {
api.on(SourceLoaderEvent, loadStoryCode);
}
}, [story]);
function handleChangeTextArea(code: string) {
setInitialCode(code, storyId);
emit(EVENT_NEW_SOURCE, code);
}
const code = story && storyId ? initialCode[storyId] : '';
return code ? (
<EditorWrapper>
<CodeEditor onChange={evt => handleChangeTextArea(evt.target.value)}>{code}</CodeEditor>
<SyntaxHighlighter language="jsx" format={false}>
{code}
</SyntaxHighlighter>
</EditorWrapper>
) : null;
};
export default Editor;