Skip to content

Commit

Permalink
component props like preview/subfield/expand should control the compo…
Browse files Browse the repository at this point in the history
…nent
  • Loading branch information
luca-simonetti committed Oct 3, 2019
1 parent 5d2293f commit a8ba392
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ interface IP {
subfield?: boolean
toolbar?: IToolbar
language?: string
addImg?: (file: File, index: number) => void
addImg?: (file: File, index: number) => void
}

interface IS {
Expand All @@ -84,9 +84,9 @@ interface IS {
class MdEditor extends React.Component<IP, IS> {
static defaultProps = {
lineNum: true,
onChange: () => {},
onSave: () => {},
addImg: () => {},
onChange: () => { },
onSave: () => { },
addImg: () => { },
fontSize: '14px',
disabled: false,
preview: false,
Expand Down Expand Up @@ -127,7 +127,7 @@ class MdEditor extends React.Component<IP, IS> {
}

componentDidUpdate(preProps: IP) {
const { value } = this.props
const { value, preview, expand, subfield } = this.props
const { history, historyIndex } = this.state
if (preProps.value !== value) {
this.reLineNum(value)
Expand All @@ -138,6 +138,15 @@ class MdEditor extends React.Component<IP, IS> {
this.saveHistory(value)
}, 500)
}
if (subfield !== preProps.subfield && this.state.subfield !== subfield) {
this.setState({ subfield });
}
if (preview !== preProps.preview && this.state.preview !== preview) {
this.setState({ preview });
}
if (expand !== preProps.expand && this.state.expand !== expand) {
this.setState({ expand });
}
}

initLanguage = (): void => {
Expand Down Expand Up @@ -273,7 +282,7 @@ class MdEditor extends React.Component<IP, IS> {
addImg = (file: File, index: number) => {
this.props.addImg(file, index)
}

$img2Url = (name: string, url: string) => {
const value = insertText(this.$vm.current, {
prefix: `![${name}](${url})`,
Expand Down

0 comments on commit a8ba392

Please sign in to comment.