Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

T/ckeditor5/1985: add synchronous image data access #99

Merged
merged 7 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/filereader.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export default class FileReader {
*/
this._reader = reader;

this._data = undefined;

/**
* Number of bytes loaded.
*
Expand All @@ -53,6 +55,16 @@ export default class FileReader {
return this._reader.error;
}

/**
* Holds the data of an already loaded file. The file must be first loaded
* by using {@link module:upload/filereader~FileReader#read `read()`}.
*
* @type {File|undefined}
*/
get data() {
return this._data;
}

/**
* Reads the provided file.
*
Expand All @@ -66,7 +78,11 @@ export default class FileReader {

return new Promise( ( resolve, reject ) => {
reader.onload = () => {
resolve( reader.result );
const result = reader.result;

this._data = result;

resolve( result );
};

reader.onerror = () => {
Expand Down
11 changes: 10 additions & 1 deletion src/filerepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,16 @@ class FileLoader {
}
}

/**
* Returns the file data. To read its data, you need for first load the file
* by using the {@link module:upload/filerepository~FileLoader#read `read()`} method.
*
* @type {File|undefined}
*/
get data() {
return this._reader.data;
}

/**
* Reads file using {@link module:upload/filereader~FileReader}.
*
Expand Down Expand Up @@ -521,7 +531,6 @@ class FileLoader {
this._filePromiseWrapper = undefined;
this._reader = undefined;
this._adapter = undefined;
this.data = undefined;
this.uploadResponse = undefined;
}

Expand Down
21 changes: 19 additions & 2 deletions tests/filereader.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,24 @@ describe( 'FileReader', () => {
expect( reader.loaded ).to.equal( 55 );
} );

describe( 'read', () => {
describe( 'data', () => {
it( 'should be undefined if file was not loaded', () => {
expect( reader.data ).to.be.undefined;
} );

it( 'should equal to loaded file data', () => {
const promise = reader.read( fileMock )
.then( () => {
expect( reader.data ).to.equal( 'File contents.' );
} );

nativeReaderMock.mockSuccess( 'File contents.' );

return promise;
} );
} );

describe( 'read()', () => {
it( 'should return a promise', () => {
expect( reader.read( fileMock ) ).to.be.instanceOf( Promise );
} );
Expand Down Expand Up @@ -81,7 +98,7 @@ describe( 'FileReader', () => {
} );
} );

describe( 'abort', () => {
describe( 'abort()', () => {
it( 'should allow to abort reading', () => {
const promise = reader.read( fileMock )
.then( () => {
Expand Down
27 changes: 27 additions & 0 deletions tests/filerepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,33 @@ describe( 'FileRepository', () => {
} );
} );

describe( 'data getter', () => {
it( 'should be undefined if no file loaded', () => {
expect( loader.data ).to.be.undefined;
} );

it( 'should return promise which resolves to a file', () => {
let resolveFile = null;

const filePromise = new Promise( resolve => {
resolveFile = resolve;
} );

const loader = fileRepository.createLoader( filePromise );

const promise = loader.read()
.then( () => {
expect( loader.data ).to.equal( 'result data' );
} );

resolveFile( createNativeFileMock() );

loader.file.then( () => nativeReaderMock.mockSuccess( 'result data' ) );

return promise;
} );
} );

describe( 'read()', () => {
it( 'should throw error when status is different than idle', () => {
loader.status = 'uploading';
Expand Down