Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
yupeng12 committed Feb 29, 2024
2 parents f9be299 + f155bee commit 08f8089
Show file tree
Hide file tree
Showing 34 changed files with 847 additions and 201 deletions.
69 changes: 69 additions & 0 deletions docs/zh-CN/concepts/event-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,75 @@ run action ajax
| copyFormat | `string` | `text/html` | 复制格式 |
| content | [模板](../../docs/concepts/template) | - | 指定复制的内容。可用 `${xxx}` 取值 |

### 打印

> 6.2.0 及以后版本
打印页面中的某个组件,对应的组件需要配置 `testid`,如果要打印多个,可以使用 `"testids": ["x", "y"]` 来打印多个组件

```schema
{
type: 'page',
body: [
{
type: 'button',
label: '打印',
level: 'primary',
className: 'mr-2',
onEvent: {
click: {
actions: [
{
actionType: 'print',
args: {
testid: 'mycrud'
}
}
]
}
}
},
{
"type": "crud",
"api": "/api/mock2/sample",
"testid": "mycrud",
"syncLocation": false,
"columns": [
{
"name": "id",
"label": "ID"
},
{
"name": "engine",
"label": "Rendering engine"
},
{
"name": "browser",
"label": "Browser"
},
{
"name": "platform",
"label": "Platform(s)"
},
{
"name": "version",
"label": "Engine version"
},
{
"name": "grade",
"label": "CSS grade"
}
]
}
]
}
```

| 属性名 | 类型 | 默认值 | 说明 |
| ------- | ---------- | ------ | ----------------- |
| testid | `string` | | 组件的 testid |
| testids | `string[]` | - | 多个组件的 testid |

### 发送邮件

通过配置`actionType: 'email'`和邮件属性实现发送邮件操作。
Expand Down
11 changes: 10 additions & 1 deletion examples/components/CRUD/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,16 @@ export default {
body: {
type: 'form',
name: 'sample-edit-form',
api: '/api/sample/$id',
data:{
env: 'test'
},
api: {
method:'post',
url:'/api/sample/$id',
messages:{
success: '成功了-${env}'
}
},
body: [
{
type: 'input-text',
Expand Down
4 changes: 2 additions & 2 deletions examples/components/PdfViewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ export default {
body: [
{
type: 'input-file',
name: 'file.test',
name: 'file',
label: '选择 PDF 文件预览效果(不会上传到服务器)',
asBlob: true,
accept: '.pdf'
},
{
type: 'pdf-viewer',
id: 'pdf-viewer',
name: 'file.test'
name: 'file'
}
]
}
Expand Down
51 changes: 51 additions & 0 deletions packages/amis-core/src/actions/PrintAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {printElements} from '../utils/printElement';
import {RendererEvent} from '../utils/renderer-event';
import {
RendererAction,
ListenerAction,
ListenerContext,
registerAction
} from './Action';

export interface IPrintAction extends ListenerAction {
actionType: 'copy';
args: {
testid?: string;
testids?: string[];
};
}

/**
* 打印动作
*
* @export
* @class PrintAction
* @implements {Action}
*/
export class PrintAction implements RendererAction {
async run(
action: IPrintAction,
renderer: ListenerContext,
event: RendererEvent<any>
) {
if (action.args?.testid) {
const element = document.querySelector(
`[data-testid='${action.args.testid}']`
);
if (element) {
printElements([element]);
}
} else if (action.args?.testids) {
const elements: Element[] = [];
action.args.testids.forEach(testid => {
const element = document.querySelector(`[data-testid='${testid}']`);
if (element) {
elements.push(element);
}
});
printElements(elements);
}
}
}

registerAction('print', new PrintAction());
1 change: 1 addition & 0 deletions packages/amis-core/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ import './EmailAction';
import './LinkAction';
import './ToastAction';
import './PageAction';
import './PrintAction';

export * from './Action';
3 changes: 2 additions & 1 deletion packages/amis-core/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ export {
splitTarget,
CustomStyle,
enableDebug,
disableDebug
disableDebug,
envOverwrite
};

export function render(
Expand Down
6 changes: 4 additions & 2 deletions packages/amis-core/src/renderers/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import LazyComponent from '../components/LazyComponent';
import {isAlive} from 'mobx-state-tree';

import type {LabelAlign} from './Item';
import {injectObjectChain} from '../utils';
import {buildTestId, injectObjectChain} from '../utils';
import {reaction} from 'mobx';

export interface FormHorizontal {
Expand Down Expand Up @@ -1808,7 +1808,8 @@ export default class Form extends React.Component<FormProps, object> {
render,
staticClassName,
static: isStatic = false,
loadingConfig
loadingConfig,
testid
} = this.props;

const {restError} = store;
Expand Down Expand Up @@ -1840,6 +1841,7 @@ export default class Form extends React.Component<FormProps, object> {
)}
onSubmit={this.handleFormSubmit}
noValidate
{...buildTestId(testid)}
>
{/* 实现回车自动提交 */}
<input type="submit" style={{display: 'none'}} />
Expand Down
6 changes: 4 additions & 2 deletions packages/amis-core/src/store/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {ServerError} from '../utils/errors';
import {normalizeApiResponseData} from '../utils/api';
import {replaceText} from '../utils/replaceText';
import {concatData} from '../utils/concatData';

import {envOverwrite} from '../envOverwrite';
import {filter} from 'amis-core';
export const ServiceStore = iRendererStore
.named('ServiceStore')
.props({
Expand Down Expand Up @@ -54,7 +55,7 @@ export const ServiceStore = iRendererStore
}

function updateMessage(msg?: string, error: boolean = false) {
self.msg = (msg && String(msg)) || '';
self.msg = (msg && filter(msg, self.data)) || '';
self.error = error;
}

Expand Down Expand Up @@ -445,6 +446,7 @@ export const ServiceStore = iRendererStore
} else {
if (json.data) {
const env = getEnv(self);
json.data = envOverwrite(json.data, env.locale);
json.data = replaceText(
json.data,
env.replaceText,
Expand Down
2 changes: 1 addition & 1 deletion packages/amis-core/src/store/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ export const TableStore = iRendererStore
typeof column.pristine.width === 'number'
? `width: ${column.pristine.width}px;`
: column.pristine.width
? `width: ${column.pristine.width};`
? `width: ${column.pristine.width};min-width: ${column.pristine.width};`
: '' // todo 可能需要让修改过列宽的保持相应宽度,目前这样相当于重置了
}`;
});
Expand Down
7 changes: 7 additions & 0 deletions packages/amis-core/src/utils/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@ export const chromeVersion = (function getChromeVersion() {
const raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
return raw ? parseInt(raw[2], 10) : false;
})();

export const isSafari =
navigator.vendor &&
navigator.vendor.indexOf('Apple') > -1 &&
navigator.userAgent &&
navigator.userAgent.indexOf('CriOS') == -1 &&
navigator.userAgent.indexOf('FxiOS') == -1;
76 changes: 76 additions & 0 deletions packages/amis-core/src/utils/printElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* 打印元素,参考 https://github.com/szepeshazi/print-elements 里的实现
* 原理就是遍历节点加上打印样式,然后打印完了再清理掉
* 对代码做了改造和优化
*/

const hideFromPrintClass = 'pe-no-print';
const preservePrintClass = 'pe-preserve-print';
const preserveAncestorClass = 'pe-preserve-ancestor';
const bodyElementName = 'BODY';

function hide(element: Element) {
if (!element.classList.contains(preservePrintClass)) {
element.classList.add(hideFromPrintClass);
}
}

function preserve(element: Element, isStartingElement: boolean) {
element.classList.remove(hideFromPrintClass);
element.classList.add(preservePrintClass);
if (!isStartingElement) {
element.classList.add(preserveAncestorClass);
}
}

function clean(element: Element) {
element.classList.remove(hideFromPrintClass);
element.classList.remove(preservePrintClass);
element.classList.remove(preserveAncestorClass);
}

function walkSiblings(element: Element, callback: (element: Element) => void) {
let sibling = element.previousElementSibling;
while (sibling) {
callback(sibling);
sibling = sibling.previousElementSibling;
}
sibling = element.nextElementSibling;
while (sibling) {
callback(sibling);
sibling = sibling.nextElementSibling;
}
}

function attachPrintClasses(element: Element, isStartingElement: boolean) {
preserve(element, isStartingElement);
walkSiblings(element, hide);
}

function cleanup(element: Element, isStartingElement: boolean) {
clean(element);
walkSiblings(element, clean);
}

function walkTree(
element: Element,
callback: (element: Element, isStartingElement: boolean) => void
) {
let currentElement: Element | null = element;
callback(currentElement, true);
currentElement = currentElement.parentElement;
while (currentElement && currentElement.nodeName !== bodyElementName) {
callback(currentElement, false);
currentElement = currentElement.parentElement;
}
}

export function printElements(elements: Element[]) {
for (let i = 0; i < elements.length; i++) {
walkTree(elements[i], attachPrintClasses);
}
window.print();
for (let i = 0; i < elements.length; i++) {
walkTree(elements[i], cleanup);
}
}
10 changes: 6 additions & 4 deletions packages/amis-editor/src/renderer/TransferTableControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export default class TransferTableOption extends React.Component<
return {
type: 'action',
actionType: 'dialog',
label: '添加表格列',
label: '设置表格列',
level: 'enhance',
dialog: {
title: '设置表格列选项',
Expand All @@ -348,12 +348,14 @@ export default class TransferTableOption extends React.Component<
{
type: 'input-text',
name: 'label',
placeholder: '标题'
placeholder: '标题',
required: true
},
{
type: 'input-text',
name: 'name',
placeholder: '绑定字段名'
placeholder: '绑定字段名',
required: true
},
{
type: 'select',
Expand Down Expand Up @@ -417,7 +419,7 @@ export default class TransferTableOption extends React.Component<
{
type: 'action',
actionType: 'dialog',
label: '添加表格行',
label: '设置表格行',
level: 'enhance',
disabled: columns && columns.length === 0,
block: true,
Expand Down
5 changes: 5 additions & 0 deletions packages/amis-ui/scss/components/_app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
height: px2rem(30px);
line-height: px2rem(30px);
font-size: px2rem(12px);

list-style: none;
margin: 0;
padding: 0 0 0 var(--gap-md);
border-bottom: var(--borderWidth) solid var(--borderColor);

a {
font-size: inherit;
}

&-item {
display: inline-block;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/amis-ui/scss/components/_print.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@media print {
.pe-no-print {
display: none !important;
}

.pe-preserve-ancestor {
display: block !important;
margin: 0 !important;
padding: 0 !important;
border: none !important;
box-shadow: none !important;
}
}

0 comments on commit 08f8089

Please sign in to comment.