Editable.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  2. import { createVNode as _createVNode } from "vue";
  3. import KeyCode from '../_util/KeyCode';
  4. import TextArea from '../input/TextArea';
  5. import EnterOutlined from "@ant-design/icons-vue/es/icons/EnterOutlined";
  6. import { defineComponent, ref, reactive, watch, onMounted, computed } from 'vue';
  7. var editableProps = function editableProps() {
  8. return {
  9. prefixCls: String,
  10. value: String,
  11. maxlength: Number,
  12. autoSize: {
  13. type: [Boolean, Object]
  14. },
  15. onSave: Function,
  16. onCancel: Function,
  17. onEnd: Function,
  18. onChange: Function,
  19. originContent: String,
  20. direction: String
  21. };
  22. };
  23. var Editable = defineComponent({
  24. compatConfig: {
  25. MODE: 3
  26. },
  27. name: 'Editable',
  28. props: editableProps(),
  29. // emits: ['save', 'cancel', 'end', 'change'],
  30. setup: function setup(props, _ref) {
  31. var emit = _ref.emit,
  32. slots = _ref.slots;
  33. var state = reactive({
  34. current: props.value || '',
  35. lastKeyCode: undefined,
  36. inComposition: false,
  37. cancelFlag: false
  38. });
  39. watch(function () {
  40. return props.value;
  41. }, function (current) {
  42. state.current = current;
  43. });
  44. var textArea = ref();
  45. onMounted(function () {
  46. if (textArea.value) {
  47. var _textArea$value;
  48. var resizableTextArea = (_textArea$value = textArea.value) === null || _textArea$value === void 0 ? void 0 : _textArea$value.resizableTextArea;
  49. var innerTextArea = resizableTextArea === null || resizableTextArea === void 0 ? void 0 : resizableTextArea.textArea;
  50. innerTextArea.focus();
  51. var length = innerTextArea.value.length;
  52. innerTextArea.setSelectionRange(length, length);
  53. }
  54. });
  55. function saveTextAreaRef(node) {
  56. textArea.value = node;
  57. }
  58. function onChange(_ref2) {
  59. var value = _ref2.target.value;
  60. state.current = value.replace(/[\r\n]/g, '');
  61. emit('change', state.current);
  62. }
  63. function onCompositionStart() {
  64. state.inComposition = true;
  65. }
  66. function onCompositionEnd() {
  67. state.inComposition = false;
  68. }
  69. function onKeyDown(e) {
  70. var keyCode = e.keyCode;
  71. if (keyCode === KeyCode.ENTER) {
  72. e.preventDefault();
  73. }
  74. // We don't record keyCode when IME is using
  75. if (state.inComposition) return;
  76. state.lastKeyCode = keyCode;
  77. }
  78. function onKeyUp(e) {
  79. var keyCode = e.keyCode,
  80. ctrlKey = e.ctrlKey,
  81. altKey = e.altKey,
  82. metaKey = e.metaKey,
  83. shiftKey = e.shiftKey;
  84. // Check if it's a real key
  85. if (state.lastKeyCode === keyCode && !state.inComposition && !ctrlKey && !altKey && !metaKey && !shiftKey) {
  86. if (keyCode === KeyCode.ENTER) {
  87. confirmChange();
  88. emit('end');
  89. } else if (keyCode === KeyCode.ESC) {
  90. state.current = props.originContent;
  91. emit('cancel');
  92. }
  93. }
  94. }
  95. function onBlur() {
  96. confirmChange();
  97. }
  98. function confirmChange() {
  99. emit('save', state.current.trim());
  100. }
  101. var textAreaClassName = computed(function () {
  102. var _ref3;
  103. return _ref3 = {}, _defineProperty(_ref3, "".concat(props.prefixCls), true), _defineProperty(_ref3, "".concat(props.prefixCls, "-edit-content"), true), _defineProperty(_ref3, "".concat(props.prefixCls, "-rtl"), props.direction === 'rtl'), _ref3;
  104. });
  105. return function () {
  106. return _createVNode("div", {
  107. "class": textAreaClassName.value
  108. }, [_createVNode(TextArea, {
  109. "ref": saveTextAreaRef,
  110. "maxlength": props.maxlength,
  111. "value": state.current,
  112. "onChange": onChange,
  113. "onKeydown": onKeyDown,
  114. "onKeyup": onKeyUp,
  115. "onCompositionstart": onCompositionStart,
  116. "onCompositionend": onCompositionEnd,
  117. "onBlur": onBlur,
  118. "rows": 1,
  119. "autoSize": props.autoSize === undefined || props.autoSize
  120. }, null), slots.enterIcon ? slots.enterIcon({
  121. className: "".concat(props.prefixCls, "-edit-content-confirm")
  122. }) : _createVNode(EnterOutlined, {
  123. "class": "".concat(props.prefixCls, "-edit-content-confirm")
  124. }, null)]);
  125. };
  126. }
  127. });
  128. export default Editable;