Skip to content

Commit 847f2a8

Browse files
committed
Move 'can format input' logic into formatter definitions
1 parent 6095286 commit 847f2a8

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/components/common/format-button.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,7 @@ export const FormatButton = (props: {
2020
const formatter = Formatters[format];
2121
const canFormat = !!formatter &&
2222
isEditorFormatter(formatter) &&
23-
[
24-
'json',
25-
'html',
26-
'xml',
27-
'javascript',
28-
'css'
29-
].includes(format);
23+
formatter.isEditApplicable;
3024

3125
return <IconButton
3226
className={props.className}

src/model/events/body-formatting.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { ImageViewer } from '../../components/editor/image-viewer';
1212
export interface EditorFormatter {
1313
language: string;
1414
cacheKey: Symbol;
15+
isEditApplicable: boolean; // Can you apply this manually during editing to format an input?
1516
render(content: Buffer): string | ObservablePromise<string>;
1617
}
1718

@@ -42,6 +43,7 @@ export const Formatters: { [key in ViewableContentType]: Formatter } = {
4243
raw: {
4344
language: 'text',
4445
cacheKey: Symbol('raw'),
46+
isEditApplicable: false,
4547
render: (input: Buffer) => {
4648
if (input.byteLength < 2000) {
4749
try {
@@ -61,38 +63,45 @@ export const Formatters: { [key in ViewableContentType]: Formatter } = {
6163
text: {
6264
language: 'text',
6365
cacheKey: Symbol('text'),
66+
isEditApplicable: false,
6467
render: (input: Buffer) => {
6568
return bufferToString(input);
6669
}
6770
},
6871
base64: {
6972
language: 'text',
7073
cacheKey: Symbol('base64'),
74+
isEditApplicable: false,
7175
render: buildAsyncRenderer('base64')
7276
},
7377
markdown: {
7478
language: 'markdown',
7579
cacheKey: Symbol('markdown'),
80+
isEditApplicable: false,
7681
render: buildAsyncRenderer('markdown')
7782
},
7883
yaml: {
7984
language: 'yaml',
8085
cacheKey: Symbol('yaml'),
86+
isEditApplicable: false,
8187
render: buildAsyncRenderer('yaml')
8288
},
8389
html: {
8490
language: 'html',
8591
cacheKey: Symbol('html'),
92+
isEditApplicable: true,
8693
render: buildAsyncRenderer('html')
8794
},
8895
xml: {
8996
language: 'xml',
9097
cacheKey: Symbol('xml'),
98+
isEditApplicable: true,
9199
render: buildAsyncRenderer('xml')
92100
},
93101
json: {
94102
language: 'json',
95103
cacheKey: Symbol('json'),
104+
isEditApplicable: true,
96105
render: (input: Buffer) => {
97106
if (input.byteLength < 10000) {
98107
const inputAsString = bufferToString(input);
@@ -118,16 +127,19 @@ export const Formatters: { [key in ViewableContentType]: Formatter } = {
118127
javascript: {
119128
language: 'javascript',
120129
cacheKey: Symbol('javascript'),
130+
isEditApplicable: true,
121131
render: buildAsyncRenderer('javascript')
122132
},
123133
css: {
124134
language: 'css',
125135
cacheKey: Symbol('css'),
136+
isEditApplicable: true,
126137
render: buildAsyncRenderer('css')
127138
},
128139
protobuf: {
129140
language: 'protobuf',
130141
cacheKey: Symbol('protobuf'),
142+
isEditApplicable: false,
131143
render: buildAsyncRenderer('protobuf')
132144
},
133145
'url-encoded': {

0 commit comments

Comments
 (0)