|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | + |
| 4 | +export class Control extends React.Component { |
| 5 | + constructor(props) { |
| 6 | + super(props); |
| 7 | + let decoded = this.decodeSlug(this.props.value); |
| 8 | + this.state = { |
| 9 | + showEdit: false, |
| 10 | + inputVal: decoded, |
| 11 | + }; |
| 12 | + |
| 13 | + this.toggleEdit = this.toggleEdit.bind(this); |
| 14 | + this.saveEdit = this.saveEdit.bind(this); |
| 15 | + this.handleChange = this.handleChange.bind(this); |
| 16 | + } |
| 17 | + |
| 18 | + slugify(str) { |
| 19 | + return str |
| 20 | + .normalize('NFD') |
| 21 | + .replace(/[\u0300-\u036f]/g, '') |
| 22 | + .toLowerCase() |
| 23 | + .trim() |
| 24 | + .replace(/[^\w^/\s-]/g, '') |
| 25 | + .replace(/[\s_-]+/g, '-') |
| 26 | + .replace(/^-+|-+$/g, ''); |
| 27 | + } |
| 28 | + |
| 29 | + isValid = () => { |
| 30 | + const { value } = this.state.inputVal; |
| 31 | + if (!value) return true; |
| 32 | + const parser = document.createElement('a'); |
| 33 | + parser.href = `http://example.com${this.prepareUrl(value)}/`; |
| 34 | + return parser.pathname !== value |
| 35 | + ? { error: { message: 'Invalid pathname.' } } |
| 36 | + : true; |
| 37 | + }; |
| 38 | + |
| 39 | + decodeSlug(val) { |
| 40 | + let prefix = this.props.field.get('prefix', ''); |
| 41 | + |
| 42 | + if (prefix) { |
| 43 | + val.replace(`/${prefix}/`, ''); |
| 44 | + } |
| 45 | + |
| 46 | + if (val == '/') { |
| 47 | + return ''; |
| 48 | + } |
| 49 | + |
| 50 | + return val |
| 51 | + .split('/') |
| 52 | + .filter((n) => n && n !== prefix) |
| 53 | + .join('/'); |
| 54 | + } |
| 55 | + |
| 56 | + toggleEdit() { |
| 57 | + this.setState((prev) => { |
| 58 | + return { |
| 59 | + showEdit: !prev.showEdit, |
| 60 | + }; |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + handleChange = (e) => { |
| 65 | + this.setState({ inputVal: e.target.value }); |
| 66 | + }; |
| 67 | + |
| 68 | + prepareUrl(val) { |
| 69 | + console.log(val); |
| 70 | + let result = val; |
| 71 | + let prefix = this.props.field.get('prefix', ''); |
| 72 | + |
| 73 | + if (val === '') { |
| 74 | + return '/'; |
| 75 | + } |
| 76 | + |
| 77 | + if (result.charAt(0) !== '/') { |
| 78 | + result = `/${result}/`; |
| 79 | + } |
| 80 | + |
| 81 | + if (prefix) { |
| 82 | + result = `/${prefix}${result}`; |
| 83 | + } |
| 84 | + |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | + saveEdit() { |
| 89 | + let slug = this.slugify(this.state.inputVal); |
| 90 | + this.setState({ |
| 91 | + showEdit: false, |
| 92 | + inputVal: slug, |
| 93 | + }); |
| 94 | + |
| 95 | + this.prepareUrl(slug); |
| 96 | + |
| 97 | + this.props.onChange(this.prepareUrl(slug)); |
| 98 | + } |
| 99 | + |
| 100 | + render() { |
| 101 | + const { field, forID, value, classNameWrapper } = this.props; |
| 102 | + const url = field.get('url', ''); |
| 103 | + const prefix = field.get('prefix', ''); |
| 104 | + |
| 105 | + return ( |
| 106 | + <div> |
| 107 | + <div style={{ display: 'flex', alignItems: 'center', gap: 4 }}> |
| 108 | + <span style={{ fontSize: 14 }}>Permalink:</span> |
| 109 | + {this.state.showEdit && ( |
| 110 | + <div style={{ display: 'flex', alignItems: 'center' }}> |
| 111 | + <div |
| 112 | + style={{ fontSize: 14, display: 'flex', alignItems: 'center' }}> |
| 113 | + {url}/ {prefix && <span>{prefix}/</span>} |
| 114 | + </div> |
| 115 | + <input |
| 116 | + id={forID} |
| 117 | + className={classNameWrapper} |
| 118 | + style={{ height: 20, padding: 5 }} |
| 119 | + value={this.state.inputVal} |
| 120 | + onChange={this.handleChange} |
| 121 | + /> |
| 122 | + <span>/</span> |
| 123 | + <button onClick={this.saveEdit}>OK</button> |
| 124 | + <button onClick={this.toggleEdit}>Cancel</button> |
| 125 | + </div> |
| 126 | + )} |
| 127 | + {!this.state.showEdit && ( |
| 128 | + <a |
| 129 | + style={{ color: 'blue', textDecoration: 'underline' }} |
| 130 | + href={`${url}${value}`} |
| 131 | + target='_blank' |
| 132 | + rel='noopener noreferrer'> |
| 133 | + {url} |
| 134 | + {value} |
| 135 | + </a> |
| 136 | + )} |
| 137 | + {!this.state.showEdit && ( |
| 138 | + <button onClick={this.toggleEdit}>Edit</button> |
| 139 | + )} |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + ); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +Control.propTypes = { |
| 147 | + onChange: PropTypes.func.isRequired, |
| 148 | + forID: PropTypes.string, |
| 149 | + value: PropTypes.node, |
| 150 | + classNameWrapper: PropTypes.string.isRequired, |
| 151 | +}; |
| 152 | + |
| 153 | +Control.defaultProps = { |
| 154 | + value: '', |
| 155 | +}; |
| 156 | + |
| 157 | +export const Widget = { |
| 158 | + // name that will be used in config.yml |
| 159 | + name: 'cc-permalink', |
| 160 | + controlComponent: Control, |
| 161 | +}; |
0 commit comments