Skip to content

Commit

Permalink
fix: use built-in FileSystemHandle types
Browse files Browse the repository at this point in the history
  • Loading branch information
christianliebel committed Apr 4, 2022
1 parent 8e6c413 commit 179e6a0
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 18 deletions.
3 changes: 1 addition & 2 deletions src/helpers/file-handling-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { FileSystemHandle } from 'browser-fs-access';
import type { DrawingContext } from '../models/drawing-context';
import { loadFileAndAdjustCanvas } from './load-file-and-adjust-canvas';

Expand All @@ -9,7 +8,7 @@ export function getLaunchImage(drawingContext: DrawingContext): void {
if (handle) {
const file = await handle.getFile();
drawingContext.document.title = file.name;
drawingContext.document.handle = handle as unknown as FileSystemHandle;
drawingContext.document.handle = handle;
await loadFileAndAdjustCanvas(file, drawingContext);
}
});
Expand Down
17 changes: 9 additions & 8 deletions src/helpers/register-drag-drop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { FileSystemHandle } from 'browser-fs-access';
import type { DrawingContext } from '../models/drawing-context';
import { loadFileAndAdjustCanvas } from './load-file-and-adjust-canvas';
import { updateDocumentContext } from './update-document-context';
Expand All @@ -22,20 +21,22 @@ export function registerDragDrop(
);
for (const file of files) {
const handle = await file.getAsFileSystemHandle();
if (handle?.kind !== 'file') {
if (!handle || !isFileHandle(handle)) {
continue;
}

const blob = await (handle as FileSystemFileHandle).getFile();
const blob = await handle.getFile();
await loadFileAndAdjustCanvas(blob, drawingContext);

updateDocumentContext(
handle as unknown as FileSystemHandle,
handle.name,
drawingContext,
);
updateDocumentContext(handle, handle.name, drawingContext);

return;
}
});
}

function isFileHandle(
handle: FileSystemHandle,
): handle is FileSystemFileHandle {
return handle.kind === 'file';
}
1 change: 0 additions & 1 deletion src/helpers/update-document-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { FileSystemHandle } from 'browser-fs-access';
import type { DrawingContext } from '../models/drawing-context';
import { updateContext } from './update-context';

Expand Down
7 changes: 1 addition & 6 deletions src/menus/file/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ import { loadFileAndAdjustCanvas } from '../../helpers/load-file-and-adjust-canv
import { updateDocumentContext } from '../../helpers/update-document-context';
import type { MenuAction } from '../../models/menu-action';
import type { DrawingContext } from '../../models/drawing-context';
import type { FileSystemHandle } from 'browser-fs-access';

export class OpenAction implements MenuAction {
async execute(drawingContext: DrawingContext): Promise<void> {
const file = await fileOpen({
extensions: ['.png'],
description: 'PNG Files',
});
updateDocumentContext(
file.handle as unknown as FileSystemHandle,
file.name,
drawingContext,
);
updateDocumentContext(file.handle, file.name, drawingContext);

await loadFileAndAdjustCanvas(file, drawingContext);
}
Expand Down
1 change: 0 additions & 1 deletion src/models/drawing-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { FileSystemHandle } from 'browser-fs-access';
import type { History } from '../helpers/history';
import type { Brush } from './brush';
import type { FillStyle } from './fill-style';
Expand Down

0 comments on commit 179e6a0

Please sign in to comment.