Skip to content

Commit

Permalink
feat: 兼容 electron 打印
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiiu committed Oct 12, 2024
1 parent bcf36db commit 21bad52
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 14 deletions.
63 changes: 55 additions & 8 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
declare module 'gm-x-printer' {
import { CSSProperties } from 'react'

interface EditorProps {
config: any
mockData: any
onSave: (config: any, isSaveAs?: boolean) => void
showEditor: boolean
addFields: any
}

interface PrinterProps {
data: any
config: any
Expand All @@ -16,69 +18,114 @@ declare module 'gm-x-printer' {
selectedRegion?: string
onReady?: () => void
}

interface BatchPrinterProps {
list: any[]
onReady?: () => void
}

interface EditorStatementProps extends EditorProps {
/** 分组,用于控制切换数据展示时显隐 */
tableFieldsGrouped?: Record<string, any>
}

// function Editor<P extends EditorProps>(props: P): React.ComponentType<P>
class Editor<T extends EditorProps> extends React.Component<T, any> {}

class EditorStockIn<T extends EditorProps> extends React.Component<T, any> {}

class EditorStockOut<T extends EditorProps> extends React.Component<T, any> {}

class EditorPurchase<T extends EditorProps> extends React.Component<T, any> {}

class EditorSettle<T extends EditorProps> extends React.Component<T, any> {}
class EditorSaleMenus<T extends (EditorProps & {
/** 是否隐藏组合商品设置 */
hideCombineSkuSetting?: boolean
})> extends React.Component<T, any> {}
class EditorAfterSales<T extends EditorProps> extends React.Component<T, any> {}

class EditorSaleMenus<
T extends EditorProps & {
/** 是否隐藏组合商品设置 */
hideCombineSkuSetting?: boolean
}
> extends React.Component<T, any> {}

class EditorAfterSales<T extends EditorProps> extends React.Component<
T,
any
> {}

class EditorCannibalize<T extends EditorProps> extends React.Component<
T,
any
> {}

class EditorMaterialRequisition<
T extends EditorProps
> extends React.Component<T, any> {}

class EditorProduction<T extends EditorProps> extends React.Component<
T,
any
> {}

class EditorStatement<T extends EditorStatementProps> extends React.Component<
T,
any
> {}

class EditorAccoutStatement<T extends EditorProps> extends React.Component<
T,
any
> {}

class EditorAccount<T extends EditorProps> extends React.Component<T, any> {}

class EditorManage<T extends EditorProps> extends React.Component<T, any> {}

class EditorBoxLabel<T extends EditorProps> extends React.Component<T, any> {}

class EditEshopOrder<T extends EditorProps> extends React.Component<T, any> {}
class EditorPurchaseDemand<T extends EditorProps> extends React.Component<T, any> {}

class EditorPurchaseDemand<T extends EditorProps> extends React.Component<
T,
any
> {}

class Printer<T extends PrinterProps> extends React.Component<T, any> {}

class BatchPrinter<T extends BatchPrinterProps> extends React.Component<
T,
any
> {}

const MULTI_SUFFIX: string

function getCSS(): string

function insertCSS(cssString: string, target?: HTMLElement | ShadowRoot): void

function doPrint(
obj: { data: any; config: any },
isTest?: boolean
isTest?: boolean,
extraConfig?: {
isPreview: boolean
isTipZoom: boolean
isPrint: boolean
isElectronPrint?: boolean
},
onReady?: () => void
): (obj: { data: any; config: any }) => Promise<any>

function doBatchPrint(
list: any[],
isTest?: boolean,
extraConfig?: { isPreview: boolean; isTipZoom: boolean, isPrint: boolean, isElectronPrint?: boolean },
extraConfig?: {
isPreview: boolean
isTipZoom: boolean
isPrint: boolean
isElectronPrint?: boolean
},
onReady?: () => void
): (list: []) => Promise<any>

export {
Editor,
EditorStockIn,
Expand Down
31 changes: 25 additions & 6 deletions src/printer/do_print.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,25 @@ function init({ isTest, isPreview, isElectronPrint, isTipZoom = true }) {
}
}

function toDoPrint({ data, config }) {
function toDoPrint({ data, config, isPrint = true, onReady, isElectronPrint }) {
return new window.Promise(resolve => {
const $app = $printer.contentWindow.document.getElementById('appContainer')
let $app
if (isElectronPrint) {
$app = document.getElementById('appContainer')
} else {
$app = $printer.contentWindow.document.getElementById('appContainer')
}
ReactDOM.unmountComponentAtNode($app)
ReactDOM.render(
<Printer
config={config}
data={data}
onReady={() => {
afterImgAndSvgLoaded(() => {
$printer.contentWindow.print()
if (isPrint) {
$printer.contentWindow.print()
}
onReady && onReady()
resolve()
}, $app)
}}
Expand Down Expand Up @@ -116,10 +124,21 @@ function toDoPrintBatch(list, isPrint = true, onReady, isElectronPrint) {
})
}

function doPrint({ data, config }, isTest) {
init({ isTest })
function doPrint({ data, config }, isTest, extraConfig, onReady) {
init({
isTest,
isElectronPrint: extraConfig?.isElectronPrint,
isPreview: extraConfig?.isPreview,
isTipZoom: extraConfig?.isTipZoom
})

return toDoPrint({ data, config })
return toDoPrint({
data,
config,
isPrint: extraConfig?.isPrint,
isElectronPrint: extraConfig?.isElectronPrint,
onReady
})
}

function doBatchPrint(
Expand Down

0 comments on commit 21bad52

Please sign in to comment.