Skip to content

Commit 27ad6e9

Browse files
committed
fix(editor-stacks/Component): add MIME type conversion for file extensions and hide specific menu buttons
1 parent 45d0c0a commit 27ad6e9

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

editor-stacks/Component.tsx

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,37 @@ const Component: FC<EditorProps> = ({
117117
let editorInstance: StacksEditor | null = null;
118118

119119
try {
120+
// Convert file extensions to MIME types for editor-stacks validation
121+
const extensionToMimeType = (ext: string): string => {
122+
const extension = ext.toLowerCase().replace(/^\./, '');
123+
const mimeTypeMap: Record<string, string> = {
124+
jpg: 'image/jpeg',
125+
jpeg: 'image/jpeg',
126+
png: 'image/png',
127+
gif: 'image/gif',
128+
webp: 'image/webp',
129+
svg: 'image/svg+xml',
130+
bmp: 'image/bmp',
131+
ico: 'image/x-icon',
132+
pdf: 'application/pdf',
133+
doc: 'application/msword',
134+
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
135+
xls: 'application/vnd.ms-excel',
136+
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
137+
ppt: 'application/vnd.ms-powerpoint',
138+
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
139+
zip: 'application/zip',
140+
rar: 'application/x-rar-compressed',
141+
txt: 'text/plain',
142+
csv: 'text/csv',
143+
};
144+
return mimeTypeMap[extension] || ext;
145+
};
146+
147+
const allowedFileTypes = uploadConfig?.allowedExtensions
148+
? uploadConfig.allowedExtensions.map(extensionToMimeType)
149+
: undefined;
150+
120151
editorInstance = new StacksEditor(containerRef.current, value || '', {
121152
placeholderText: placeholder || t('placeholder', ''),
122153
parserFeatures: {
@@ -127,7 +158,7 @@ const Component: FC<EditorProps> = ({
127158
? {
128159
handler: imageUploadHandler,
129160
sizeLimitMib: uploadConfig?.maxImageSizeMiB,
130-
acceptedFileTypes: uploadConfig?.allowedExtensions,
161+
acceptedFileTypes: allowedFileTypes,
131162
}
132163
: undefined,
133164
editorHelpLink: 'https://stackoverflow.com/editing-help',
@@ -212,10 +243,22 @@ const Component: FC<EditorProps> = ({
212243
}, [value]);
213244

214245
return (
215-
<div
216-
className="editor-stacks-wrapper editor-stacks-scope"
217-
ref={containerRef}
218-
/>
246+
<>
247+
<style>{`
248+
/* Hide specific menu buttons */
249+
.editor-stacks-wrapper [data-key="tag-btn"],
250+
.editor-stacks-wrapper [data-key="meta-tag-btn"],
251+
.editor-stacks-wrapper [data-key="spoiler-btn"],
252+
.editor-stacks-wrapper [data-key="subscript-btn"],
253+
.editor-stacks-wrapper [data-key="superscript-btn"] {
254+
display: none !important;
255+
}
256+
`}</style>
257+
<div
258+
className="editor-stacks-wrapper editor-stacks-scope"
259+
ref={containerRef}
260+
/>
261+
</>
219262
);
220263
};
221264

0 commit comments

Comments
 (0)