Skip to content

Commit 8530488

Browse files
authored
Merge pull request #380 from marp-team/mac-preview
Improve the behavior of preview mode on macOS
2 parents 17359df + 98e227c commit 8530488

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Improve an activation behavior from dock in preview mode on macOS ([#380](https://github.com/marp-team/marp-cli/pull/380))
8+
9+
### Changed
10+
11+
- Update Dock icon in preview mode on macOS to suit for macOS Big Sur ([#380](https://github.com/marp-team/marp-cli/pull/380))
12+
513
## v1.3.2 - 2021-08-18
614

715
### Fixed

rollup.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ const plugins = (opts = {}) => [
5252
],
5353
}),
5454
pugPlugin({ pugRuntime: 'pug-runtime' }),
55-
url({ sourceDir: path.join(__dirname, 'lib') }),
55+
url({
56+
sourceDir: path.join(__dirname, 'lib'),
57+
limit: 30720,
58+
}),
5659
compact &&
5760
terser({
5861
keep_classnames: /^CLIError$/,

src/assets/mac-dock-icon.png

26.2 KB
Loading

src/preview.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { EventEmitter } from 'events'
33
import { nanoid } from 'nanoid'
44
import puppeteer from 'puppeteer-core'
55
import TypedEmitter from 'typed-emitter'
6-
import favicon from './assets/favicon.png'
6+
import macDockIcon from './assets/mac-dock-icon.png'
77
import { ConvertType, mimeTypes } from './converter'
88
import { error } from './error'
99
import { File, FileType } from './file'
@@ -27,6 +27,12 @@ export namespace Preview {
2727
height: number
2828
width: number
2929
}
30+
31+
export interface Window extends EventEmitter {
32+
page: puppeteer.Page
33+
close: () => Promise<void>
34+
load: (uri: string) => Promise<void>
35+
}
3036
}
3137

3238
export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Events>) {
@@ -67,7 +73,7 @@ export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Eve
6773
}
6874
}
6975

70-
private createWindowObject(page: puppeteer.Page) {
76+
private createWindowObject(page: puppeteer.Page): Preview.Window {
7177
const window = new EventEmitter()
7278

7379
page.on('close', async () => window.emit('close'))
@@ -77,7 +83,7 @@ export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Eve
7783
close: async () => {
7884
try {
7985
return await page.close()
80-
} catch (e) {
86+
} catch (e: any) {
8187
// Ignore raising error if a target page has already close
8288
/* istanbul ignore next */
8389
if (!e.message.includes('Target closed.')) throw e
@@ -133,7 +139,7 @@ export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Eve
133139
}
134140
}
135141

136-
private async launch() {
142+
private async launch(): Promise<Preview.Window> {
137143
const baseArgs = generatePuppeteerLaunchArgs()
138144

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

153-
// Set Marp icon asynchrnously
159+
// Set Marp icon asynchrnously (only for macOS)
154160
this.puppeteerInternal
155161
.target()
156162
.createCDPSession()
157163
.then((session) => {
158164
session
159-
.send('Browser.setDockTile', { image: favicon.slice(22) })
165+
.send('Browser.setDockTile', { image: macDockIcon.slice(22) })
160166
.catch(() => {
161167
// No ops
162168
})
@@ -173,11 +179,20 @@ export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Eve
173179
})
174180

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

183+
let windowObject: Preview.Window | undefined
184+
185+
/* istanbul ignore next */
186+
if (process.platform === 'darwin') {
187+
// An initial app window is not using in macOS for correct handling activation from Dock
188+
windowObject = (await this.createWindow()) || undefined
189+
await page.close()
190+
}
191+
192+
page.on('close', handlePageOnClose)
178193
this.emit('launch')
179194

180-
return this.createWindowObject(page)
195+
return windowObject || this.createWindowObject(page)
181196
}
182197
}
183198

0 commit comments

Comments
 (0)