mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-31 23:28:34 -04:00
Move toolbar functionality from redux to codemirror dispatch (#2083)
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
a8bd22aef3
commit
e93607c96e
99 changed files with 1730 additions and 1721 deletions
18
src/utils/read-file.test.ts
Normal file
18
src/utils/read-file.test.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { FileContentFormat, readFile } from './read-file'
|
||||
|
||||
describe('read file', () => {
|
||||
it('reads files as text', async () => {
|
||||
const a = await readFile(new Blob(['Kinderriegel'], { type: 'text/plain' }), FileContentFormat.TEXT)
|
||||
expect(a).toBe('Kinderriegel')
|
||||
})
|
||||
it('reads files as data url', async () => {
|
||||
const a = await readFile(new Blob(['Kinderriegel'], { type: 'text/plain' }), FileContentFormat.DATA_URL)
|
||||
expect(a).toBe('data:text/plain;base64,S2luZGVycmllZ2Vs')
|
||||
})
|
||||
})
|
40
src/utils/read-file.ts
Normal file
40
src/utils/read-file.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
export enum FileContentFormat {
|
||||
TEXT,
|
||||
DATA_URL
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the given {@link File}.
|
||||
*
|
||||
* @param file The file to read
|
||||
* @param fileReaderMode Defines as what the file content should be formatted.
|
||||
* @throws Error if an invalid read mode was given or if the file couldn't be read.
|
||||
* @return the file content
|
||||
*/
|
||||
export const readFile = async (file: Blob, fileReaderMode: FileContentFormat): Promise<string> => {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const fileReader = new FileReader()
|
||||
fileReader.addEventListener('load', () => {
|
||||
resolve(fileReader.result as string)
|
||||
})
|
||||
fileReader.addEventListener('error', (error) => {
|
||||
reject(error)
|
||||
})
|
||||
switch (fileReaderMode) {
|
||||
case FileContentFormat.DATA_URL:
|
||||
fileReader.readAsDataURL(file)
|
||||
break
|
||||
case FileContentFormat.TEXT:
|
||||
fileReader.readAsText(file)
|
||||
break
|
||||
default:
|
||||
throw new Error('Unknown file reader mode')
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue