Skip to content

Commit

Permalink
Merge pull request #380 from marp-team/mac-preview
Browse files Browse the repository at this point in the history
Improve the behavior of preview mode on macOS
  • Loading branch information
yhatt authored Aug 25, 2021
2 parents 17359df + 98e227c commit 8530488
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## [Unreleased]

### Fixed

- Improve an activation behavior from dock in preview mode on macOS ([#380](https://github.com/marp-team/marp-cli/pull/380))

### Changed

- Update Dock icon in preview mode on macOS to suit for macOS Big Sur ([#380](https://github.com/marp-team/marp-cli/pull/380))

## v1.3.2 - 2021-08-18

### Fixed
Expand Down
5 changes: 4 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ const plugins = (opts = {}) => [
],
}),
pugPlugin({ pugRuntime: 'pug-runtime' }),
url({ sourceDir: path.join(__dirname, 'lib') }),
url({
sourceDir: path.join(__dirname, 'lib'),
limit: 30720,
}),
compact &&
terser({
keep_classnames: /^CLIError$/,
Expand Down
Binary file added src/assets/mac-dock-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 23 additions & 8 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EventEmitter } from 'events'
import { nanoid } from 'nanoid'
import puppeteer from 'puppeteer-core'
import TypedEmitter from 'typed-emitter'
import favicon from './assets/favicon.png'
import macDockIcon from './assets/mac-dock-icon.png'
import { ConvertType, mimeTypes } from './converter'
import { error } from './error'
import { File, FileType } from './file'
Expand All @@ -27,6 +27,12 @@ export namespace Preview {
height: number
width: number
}

export interface Window extends EventEmitter {
page: puppeteer.Page
close: () => Promise<void>
load: (uri: string) => Promise<void>
}
}

export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Events>) {
Expand Down Expand Up @@ -67,7 +73,7 @@ export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Eve
}
}

private createWindowObject(page: puppeteer.Page) {
private createWindowObject(page: puppeteer.Page): Preview.Window {
const window = new EventEmitter()

page.on('close', async () => window.emit('close'))
Expand All @@ -77,7 +83,7 @@ export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Eve
close: async () => {
try {
return await page.close()
} catch (e) {
} catch (e: any) {
// Ignore raising error if a target page has already close
/* istanbul ignore next */
if (!e.message.includes('Target closed.')) throw e
Expand Down Expand Up @@ -133,7 +139,7 @@ export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Eve
}
}

private async launch() {
private async launch(): Promise<Preview.Window> {
const baseArgs = generatePuppeteerLaunchArgs()

this.puppeteerInternal = await launchPuppeteer({
Expand All @@ -150,13 +156,13 @@ export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Eve
}),
})

// Set Marp icon asynchrnously
// Set Marp icon asynchrnously (only for macOS)
this.puppeteerInternal
.target()
.createCDPSession()
.then((session) => {
session
.send('Browser.setDockTile', { image: favicon.slice(22) })
.send('Browser.setDockTile', { image: macDockIcon.slice(22) })
.catch(() => {
// No ops
})
Expand All @@ -173,11 +179,20 @@ export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Eve
})

const [page] = await this.puppeteerInternal.pages()
page.on('close', handlePageOnClose)

let windowObject: Preview.Window | undefined

/* istanbul ignore next */
if (process.platform === 'darwin') {
// An initial app window is not using in macOS for correct handling activation from Dock
windowObject = (await this.createWindow()) || undefined
await page.close()
}

page.on('close', handlePageOnClose)
this.emit('launch')

return this.createWindowObject(page)
return windowObject || this.createWindowObject(page)
}
}

Expand Down

0 comments on commit 8530488

Please sign in to comment.