diff --git a/demo/index.tsx b/demo/index.tsx index 44688b5..c2f7d68 100644 --- a/demo/index.tsx +++ b/demo/index.tsx @@ -129,9 +129,17 @@ class App extends React.Component> { onClose={() => { this.setState({ visible: false }); } } images={images} activeIndex={this.state.activeIndex} - attribute={false} container={inline ? this.container : null} downloadable + customToolbar={(toolbars) => { + return toolbars.concat([{ + key: 'test', + render:
C
, + onClick: (activeImage) => { + console.log(activeImage); + }, + }]); + }} />
diff --git a/src/ViewerCore.tsx b/src/ViewerCore.tsx index 417baa3..d2893f3 100644 --- a/src/ViewerCore.tsx +++ b/src/ViewerCore.tsx @@ -2,8 +2,8 @@ import * as React from 'react'; import './style/index.less'; import ViewerCanvas from './ViewerCanvas'; import ViewerNav from './ViewerNav'; -import ViewerToolbar from './ViewerToolbar'; -import ViewerProps, { ImageDecorator } from './ViewerProps'; +import ViewerToolbar, { defaultToolbars } from './ViewerToolbar'; +import ViewerProps, { ImageDecorator, ToolbarConfig } from './ViewerProps'; import Icon, { ActionType } from './Icon'; function noop() {} @@ -41,6 +41,7 @@ export default class ViewerCore extends React.Component toolbars, }; private prefixCls: string; @@ -228,7 +229,7 @@ export default class ViewerCore extends React.Component { switch (type) { case ActionType.prev: if (this.state.activeIndex - 1 >= 0) { @@ -271,6 +272,15 @@ export default class ViewerCore extends React.Component { const activeImage = this.getActiveImage(); if (activeImage.downloadUrl) { @@ -380,29 +390,29 @@ export default class ViewerCore extends React.Component )} {this.props.noNavbar || ( diff --git a/src/ViewerProps.ts b/src/ViewerProps.ts index e8b525b..3ffc831 100644 --- a/src/ViewerProps.ts +++ b/src/ViewerProps.ts @@ -4,6 +4,13 @@ export interface ImageDecorator { downloadUrl?: string; } +export interface ToolbarConfig { + key: string; + actionType?: number; + render?: React.ReactNode; + onClick?: (activeImage: ImageDecorator) => void; +} + interface ViewerProps { /** viewer是否可见 */ visible?: boolean; @@ -49,6 +56,9 @@ interface ViewerProps { // wheather to show change button changeable?: boolean; + + // custom toolbar + customToolbar?: (toolbars: ToolbarConfig[]) => ToolbarConfig[]; } export default ViewerProps; diff --git a/src/ViewerToolbar.tsx b/src/ViewerToolbar.tsx index 8919f6c..4bcc568 100644 --- a/src/ViewerToolbar.tsx +++ b/src/ViewerToolbar.tsx @@ -1,9 +1,10 @@ import * as React from 'react'; import Icon, { ActionType } from './Icon'; +import { ToolbarConfig } from './ViewerProps'; export interface ViewerToolbarProps { prefixCls: string; - onAction: (type: ActionType) => void; + onAction: (config: ToolbarConfig) => void; alt: string; width: number; height: number; @@ -14,6 +15,56 @@ export interface ViewerToolbarProps { changeable: boolean; downloadable: boolean; noImgDetails: boolean; + toolbars: ToolbarConfig[]; +} + +export const defaultToolbars: ToolbarConfig[] = [ + { + key: 'zoomIn', + actionType: ActionType.zoomIn, + }, + { + key: 'zoomOut', + actionType: ActionType.zoomOut, + }, + { + key: 'prev', + actionType: ActionType.prev, + }, + { + key: 'reset', + actionType: ActionType.reset, + }, + { + key: 'next', + actionType: ActionType.next, + }, + { + key: 'rotateLeft', + actionType: ActionType.rotateLeft, + }, + { + key: 'rotateRight', + actionType: ActionType.rotateRight, + }, + { + key: 'scaleX', + actionType: ActionType.scaleX, + }, + { + key: 'scaleY', + actionType: ActionType.scaleY, + }, + { + key: 'download', + actionType: ActionType.download, + }, +]; + +function deleteToolbarFromKey(toolbars: ToolbarConfig[], keys: string[]) { + const targetToolbar = toolbars.filter(item => keys.indexOf(item.key) < 0); + + return targetToolbar; } export default class ViewerToolbar extends React.Component { @@ -22,8 +73,29 @@ export default class ViewerToolbar extends React.Component { + let content = null; + // default toolbar + if (typeof ActionType[config.actionType] !== 'undefined') { + content = ; + } + // extra toolbar + if (config.render) { + content = config.render; + } + return ( +
  • {this.handleAction(config);}} + > + {content} +
  • + ); } render() { @@ -33,96 +105,29 @@ export default class ViewerToolbar extends React.Component ) : null; - let featureNodeArr = []; - if (this.props.zoomable) { - featureNodeArr = featureNodeArr.concat([ -
  • {this.handleAction(ActionType.zoomIn);}}> - -
  • , -
  • {this.handleAction(ActionType.zoomOut);}}> - -
  • , - ]); + let toolbars = this.props.toolbars; + if (!this.props.zoomable) { + toolbars = deleteToolbarFromKey(toolbars, ['zoomIn', 'zoomOut']); } - const resetTool = ( -
  • {this.handleAction(ActionType.reset);}}> - -
  • - ); - if (this.props.changeable) { - featureNodeArr = featureNodeArr.concat([ -
  • {this.handleAction(ActionType.prev);}}> - -
  • , - resetTool, -
  • {this.handleAction(ActionType.next);}}> - -
  • , - ]); - } else { - featureNodeArr = featureNodeArr.concat([ - resetTool, - ]); + if (!this.props.changeable) { + toolbars = deleteToolbarFromKey(toolbars, ['prev', 'next']); } - if (this.props.rotatable) { - featureNodeArr = featureNodeArr.concat([ -
  • {this.handleAction(ActionType.rotateLeft);}}> - -
  • , -
  • {this.handleAction(ActionType.rotateRight);}}> - -
  • , - ]); + if (!this.props.rotatable) { + toolbars = deleteToolbarFromKey(toolbars, ['rotateLeft', 'rotateRight']); } - if (this.props.scalable) { - featureNodeArr = featureNodeArr.concat([ -
  • {this.handleAction(ActionType.scaleX);}}> - -
  • , -
  • {this.handleAction(ActionType.scaleY);}}> - -
  • , - ]); + if (!this.props.scalable) { + toolbars = deleteToolbarFromKey(toolbars, ['scaleX', 'scaleY']); } - if (this.props.downloadable) { - featureNodeArr = featureNodeArr.concat([ -
  • {this.handleAction(ActionType.download);}}> - -
  • , - ]); + if (!this.props.downloadable) { + toolbars = deleteToolbarFromKey(toolbars, ['download']); } return (
    {attributeNode}
      - {featureNodeArr} + {toolbars.map(item => { + return this.renderAction(item); + })}
    ); diff --git a/src/style/index.less b/src/style/index.less index b8109b1..d213bc7 100644 --- a/src/style/index.less +++ b/src/style/index.less @@ -52,6 +52,7 @@ &-btn { background-color: @btn-background-color; + color: white; } &-btn:hover{ @@ -132,7 +133,7 @@ border-radius: @toolbarHeight; margin-right: 3px; cursor: pointer; - line-height: 29px; + line-height: 28px; } &-toolbar li:hover {