Skip to content

Commit

Permalink
Merge 62e9233 into d693814
Browse files Browse the repository at this point in the history
  • Loading branch information
inukshuk committed May 11, 2022
2 parents d693814 + 62e9233 commit 2b01dff
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 18 deletions.
6 changes: 6 additions & 0 deletions res/menu/app.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ en:
submenu:
- label: 'JSON-LD'
command: 'app:export-item'
- label: 'PDF'
submenu:
- label: 'Portrait'
command: 'app:print-pdf'
- label: 'Landscape'
command: 'app:print-pdf-landscape'
- type: 'separator'
- &consolidate
label: 'Consolidate Photo Library'
Expand Down
12 changes: 8 additions & 4 deletions res/menu/context.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,15 @@ en:
item-read-only: &item-read-only
- label: 'Export Item'
id: 'export'
submenu:
submenu: &export-submenu
- label: 'JSON-LD'
command: 'app:export-item'
- label: 'PDF'
submenu:
- label: 'Portrait'
command: 'app:print-pdf'
- label: 'Landscape'
command: 'app:print-pdf-landscape'
- label: 'Export Item Notes …'
command: 'app:export-notes'
- label: 'Copy Item Link'
Expand All @@ -197,9 +203,7 @@ en:
item-bulk-read-only: &item-bulk-read-only
- label: 'Export Selected Items'
id: 'export'
submenu:
- label: 'JSON-LD'
command: 'app:export-item'
submenu: *export-submenu
- label: 'Export Selected Item Notes …'
command: 'app:export-notes'
- label: 'Copy Selected Item Notes'
Expand Down
1 change: 1 addition & 0 deletions res/strings/browser.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ en:
file:
project: Tropy Projects
plugin: Tropy Plugins
pdf: PDF files
crashed:
buttons:
- Close Window
Expand Down
1 change: 1 addition & 0 deletions res/strings/renderer.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ en:
}
load: Loading items …
merge: Merging item …
print: Processing items …
restore: Restoring item …
save: Saving items …
tag:
Expand Down
1 change: 1 addition & 0 deletions src/browser/actions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as activity } from '../actions/activity'
export { default as api } from '../actions/api'
export { default as cache } from '../actions/cache'
export { default as context } from '../actions/context'
Expand Down
10 changes: 9 additions & 1 deletion src/browser/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ const Dialog = {
})
},

save(win, opts) {
return Dialog.show('save', win, opts)
},

show(type, win, opts) {
switch (type) {
case 'save':
return dialog
.showSaveDialog(win, { defaultPath, ...opts })
.showSaveDialog(win, {
defaultPath,
properties: ['createDirectory'],
...opts
})
.then(({ filePath }) => {
if (filePath) {
defaultPath = dirname(filePath)
Expand Down
33 changes: 33 additions & 0 deletions src/browser/papersize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { exec } from '../common/spawn'
import { read } from './mac-defaults'
import { warn } from '../common/log'

const DARWIN_PAPER = {
'iso-a3': 'A3',
'iso-a4': 'A4',
'iso-a5': 'A5',
'na-legal': 'Legal',
'na-letter': 'Letter',
'tabloid': 'Tabloid'
}

export async function papersize() {
try {
switch (process.platform) {
case 'linux':
return (await exec('paperconf -N')).stdout.trim()

case 'darwin':
return DARWIN_PAPER[
await read('org.cups.PrintingPrefs', 'DefaultPaperID', 'string')
] || 'A4'

case 'win32':
return 'A4'
}
} catch (e) {
warn(`failed to fetch system default paper size: ${e.message}`)
}

return 'A4'
}
41 changes: 37 additions & 4 deletions src/browser/tropy.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,17 @@ export class Tropy extends EventEmitter {
this.dispatch(act.item.print(), this.wm.current())
})

this.on('app:print-pdf', () => {
this.dispatch(act.item.print(null, { pdf: true }), this.wm.current())
})

this.on('app:print-pdf-landscape', () => {
this.dispatch(act.item.print(null, {
pdf: true,
landscape: true
}), this.wm.current())
})

this.on('app:zoom-in', () => {
this.state.zoom = this.wm.zoom(this.state.zoom + 0.25)
})
Expand Down Expand Up @@ -789,7 +800,7 @@ export class Tropy extends EventEmitter {
this.emit(cmd, BrowserWindow.fromWebContents(event.sender), ...args)
})

ipc.on('print', async (_, opts) => {
ipc.handle('print', async (_, opts) => {
try {
if (!opts.items.length) return

Expand All @@ -808,9 +819,31 @@ export class Tropy extends EventEmitter {
delay(60000)
])

debug('will open print dialog')
let status = await WindowManager.print(win)
info(`print status: ${status}`)
if (opts.pdf) {
let path = await dialog.save(win, {
filters: [{
name: this.dict.dialog.file.pdf,
extensions: ['pdf']
}]
})

if (!path) {
info('pdf printing cancelled: no file selected')
return
}

info('will print pdf')
await WindowManager.printToPDF(win, path, {
landscape: opts.landscape
})

info(`saved pdf ${path}`)

} else {
debug('will open print dialog')
let status = await WindowManager.print(win)
info(`print status: ${status}`)
}

} finally {
if (win != null) win.destroy()
Expand Down
19 changes: 19 additions & 0 deletions src/browser/wm.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import assert from 'assert'
import { EventEmitter } from 'events'
import { join } from 'path'
import { URL } from 'url'
import { read } from './mac-defaults'
import { papersize } from './papersize'
import dialog from './dialog'
import { debug, error, trace, warn } from '../common/log'
import { darwin } from '../common/os'
import { channel, paths } from '../common/release'
import { Icon, View } from '../common/res'
import { SASS } from '../constants'
import { writeFile } from 'fs/promises'

import {
array,
Expand Down Expand Up @@ -583,6 +586,22 @@ export class WindowManager extends EventEmitter {
})
}

static async printToPDF(win, path, { pageSize, landscape = false } = {}) {
assert(path?.length > 0, 'missing PDF output file')

if (!pageSize)
pageSize = await papersize()

let data = await win.webContents.printToPDF({
pageSize,
landscape
})

await writeFile(path, data)

return path
}

static async shouldReduceTransparency() {
try {
// TODO re-enable vibrancy effect when sidebar scrolling
Expand Down
12 changes: 10 additions & 2 deletions src/commands/item/print.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { ipcRenderer as ipc } from 'electron'
import { select } from 'redux-saga/effects'
import { call, select } from 'redux-saga/effects'
import { Command } from '../command'
import { ITEM } from '../../constants'
import { getPrintableItems } from '../../selectors'


export class Print extends Command {
*exec() {
let { pdf, landscape } = this.action.meta

let [prefs, project, items] = yield select(state => ([
state.settings.print,
state.project.id,
getPrintableItems(state)
]))

if (items.length) {
ipc.send('print', { ...prefs, project, items })
yield call(ipc.invoke, 'print', {
pdf,
landscape,
...prefs,
project,
items
})
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/common/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import ChildProcess from 'child_process'

export const execFile = promisify(ChildProcess.execFile)

export const exec = promisify(ChildProcess.exec)

export async function spawn(cmd, args) {
try {
let child = ChildProcess.spawn(cmd, args)
Expand Down
7 changes: 0 additions & 7 deletions src/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ save.project = (opts) => save({
name: t('dialog', 'filter', 'projects'),
extensions: ['tpy']
}],
properties: ['createDirectory'],
...opts
})

Expand All @@ -173,7 +172,6 @@ save.csv = (opts) => save({
name: t('dialog', 'filter', 'csv'),
extensions: ['csv']
}],
properties: ['createDirectory'],
...opts
})

Expand All @@ -182,7 +180,6 @@ save.template = (opts) => save({
name: t('dialog', 'filter', 'templates'),
extensions: ['ttp']
}],
properties: ['createDirectory'],
...opts
})

Expand All @@ -191,7 +188,6 @@ save.items = (opts) => save({
name: t('dialog', 'filter', 'jsonld'),
extensions: ['json', 'jsonld']
}],
properties: ['createDirectory'],
...opts
})

Expand All @@ -200,7 +196,6 @@ save.image = (opts) => save({
name: t('dialog', 'filter', 'images'),
extensions: ['jpg', 'jpeg', 'png', 'webp']
}],
properties: ['createDirectory'],
...opts
})

Expand All @@ -209,7 +204,6 @@ save.vocab = (opts) => save({
name: t('dialog', 'filter', 'rdf'),
extensions: ['n3']
}],
properties: ['createDirectory'],
...opts
})

Expand All @@ -218,7 +212,6 @@ save.notes = (opts) => save({
name: t('dialog', 'filter', 'notes'),
extensions: ['json', 'jsonld', 'md', 'markdown', 'html', 'txt']
}],
properties: ['createDirectory'],
...opts
})

Expand Down

0 comments on commit 2b01dff

Please sign in to comment.