• CodeMirror实现类似input框的 placeholder提示效果


    一、需求

            最近在工作时遇到一个需求,要求在codemirror插件中实现一个类似于input框的placeholder效果,用来提示yaml文件如何书写,菜鸡博主忙活了一天多找到了一个实现这种效果的方法,下面展示如何实现这种效果。

    二、关键函数

            首先默认你看过我的

    React中CodeMirror插件的使用及封装_大聪明码农徐的博客-CSDN博客_codemirror react关于 CodeMirror 插件的引入、解释、使用、及封装。https://blog.csdn.net/qq_45799465/article/details/125970339?spm=1001.2014.3001.5501        的这篇文章,对于codemirror有一个初步的认识,并能够理解其基本使用。下面介绍关键实现函数。

    (一)focus 聚焦方法

    1. focusEvent = () => {
    2. const { data, TooltipValue, bool } = this.props
    3. const { CodeMirrorRef } = this;
    4. const editor = CodeMirrorRef.getCodeMirror();
    5. const str = editor.getValue()
    6. if (bool) {
    7. if (str == TooltipValue) {
    8. editor.setValue('')
    9. } else {
    10. editor.setValue(str)
    11. }
    12. }
    13. }

    (二)blur 失焦方法

    1. blurEvent = () => {
    2. const { data, TooltipValue, bool } = this.props;
    3. const { CodeMirrorRef } = this;
    4. const editor = CodeMirrorRef.getCodeMirror();
    5. const str = editor.getValue()
    6. if (bool) {
    7. if (str.length == 0) {
    8. editor.setValue(TooltipValue)
    9. } else {
    10. editor.setValue(str)
    11. }
    12. }
    13. }

    (三)挂载方法

    通过 ref 拿到 codemirror的dom节点。

    节点.on("事件",“函数”)         //挂载到节点上

    节点.off("事件",“函数”)         //取消挂载

    1. componentDidMount() {
    2. const { bool } = this.props;
    3. const { CodeMirrorRef } = this;
    4. const editor = CodeMirrorRef.getCodeMirror();
    5. if (bool) {
    6. editor.on("focus", this.focusEvent)
    7. editor.on("blur", this.blurEvent)
    8. }
    9. }

    三、需求实现

            看完上面的可能不太懂,下面我来解读一下。

            这里为了兼顾修改的功能,我把文字提示和传进去的文件分别用了两个变量。

            这里 data和tooltipValue是完全不一样的两个文件。

            data是动态传的值,tooltipValue是提示的一个数组对应的值。

    1. TooltipValueArr: {
    2. affinity:'#示例\n#nodeAffinity:\n# preferredDuringSchedulingIgnoredDuringExecution:\n# - weight: 1\n# preference:\n# matchExpressions:\n# - key: disktype\n# operator: In\n# values:\n# - ssd\n',
    3. tolerations:'#示例\n#- key: "test"\n# operator: "Equal"\n# value: "yk"\n# effect: "NoSchedule"\n',
    4. env:'#示例\n#- name: NGINX_USERNAEM\n# valueFrom:\n# secretKeyRef:\n# key: username\# name: test-secret\n# optional: false\n#- name: NGINX_PASSWORD\# valueFrom:\n# secretKeyRef:\n# key: password\n# name: test-secret\n# optional: false\n#- name: MY_POD_IP\n# valueFrom:\n# fieldRef:\# fieldPath: status.podIP\n',
    5. volumes:'#示例\n#- hostPath:\n# path: /test\n# name: data\n#- name: mydata\n# persistentVolumeClaim:\n# claimName: test-pvc\n#- configMap:\n# name: test\n# name: config\n',
    6. volumeMounts:'#示例\n#- mountPath: /opt\n# name: data\n#- mountPath: /etc/test/conf/aa\n# name: mydata\n# subPath: aa\n#- mountPath: /etc/test/conf/nginx.conf\n# name: config\n# subPath: test.conf\n'
    7. },

             bool 是判断是否为新增,新增就有提示,修改则不用去动。

    四、详细代码

    1. //封装组建内新增的
    2. componentDidMount() {
    3. const { bool } = this.props;
    4. const { CodeMirrorRef } = this;
    5. const editor = CodeMirrorRef.getCodeMirror();
    6. if (bool) {
    7. editor.on("focus", this.focusEvent)
    8. editor.on("blur", this.blurEvent)
    9. }
    10. }
    11. saveRef = ref => {
    12. this.CodeMirrorRef = ref;
    13. const { saveRef = false } = this.props;
    14. if (saveRef) {
    15. saveRef(ref);
    16. }
    17. };
    18. focusEvent = () => {
    19. const { data, TooltipValue, bool } = this.props
    20. const { CodeMirrorRef } = this;
    21. const editor = CodeMirrorRef.getCodeMirror();
    22. const str = editor.getValue()
    23. if (bool) {
    24. if (str == TooltipValue) {
    25. editor.setValue('')
    26. } else {
    27. editor.setValue(str)
    28. }
    29. }
    30. }
    31. blurEvent = () => {
    32. const { data, TooltipValue, bool } = this.props;
    33. const { CodeMirrorRef } = this;
    34. const editor = CodeMirrorRef.getCodeMirror();
    35. const str = editor.getValue()
    36. if (bool) {
    37. if (str.length == 0) {
    38. editor.setValue(TooltipValue)
    39. } else {
    40. editor.setValue(str)
    41. }
    42. }
    43. }
    44. //jsx语法中引用的的代码
    45. <CodeMirrorForm
    46. setFieldsValue={setFieldsValue}
    47. formItemLayout={formItemLayoutss}
    48. Form={Form}
    49. style={{ marginBottom: '20px' }}
    50. getFieldDecorator={getFieldDecorator}
    51. name={selectVal}
    52. message="请编辑内容"
    53. data={yamlValue || ''}
    54. mode={'yaml'}
    55. bool = { isBool }
    56. TooltipValue={ TooltipValue }
    57. />

    五、效果展示

     

  • 相关阅读:
    Qt中的事件处理
    pytgon代码改错
    gcc编译C语言
    如何用 Java 写一个 Java 虚拟机
    【零散技术】10分钟学会Odoo导出自定义excel报表
    Swagger之学习使用
    [算法应用]关键路径算法的简单应用
    Nginx重写功能和反向代理
    数据分批拆分
    常见的编码及哈希算法
  • 原文地址:https://blog.csdn.net/qq_45799465/article/details/126183158