react 使用 ace.js

1
yarn add react-ace
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import React, { useEffect, useState, useRef } from "react";
import { message } from "antd";

import { useLogin } from "../../hooks/useLogin";
import { Nav } from 'react-bootstrap'
import firstPage from './firstPage.module.css'
import { GetSpiderScript } from "../../api/spiders";

import AceEditor from "react-ace";

import "ace-builds/src-noconflict/mode-javascript";
import "ace-builds/src-noconflict/theme-monokai";

import $ from 'jquery'
import 'jquery-mousewheel'
export default function FirstPage({
  history
}) {
  const { login, loading } = useLogin()
  useEffect(() => {
    if (!loading) {
      if (!login) {
        message.warn('用户还没有登录')
        setTimeout(() => {
          history.replace('login')
        }, 2000)
      }
    }
  }, [loading])
  const [scriptList, setScriptList] = useState([])
  useEffect(() => {
    GetSpiderScript().then(({ json, code, r }) => {
      let {
        list
      } = json;
      setScriptList(list)

    })
  }, [])

  const onChange = (e) => {
    console.log(e, ref)
  }
  const ref = useRef(null)
  // const [fontSize,setFontSize] = useState(18)
  useEffect(() => {
    // window._font_size = 18
    console.log('__init __')
    const handle = () => {
      $('._current_ace').mousewheel(function (event) {
        // console.log(event)
        // return
        let editor = ref.current.editor
        // let _font_size = fontSize
        // console.log(_font_size)
        if (!event.ctrlKey) {
         
          //滚动
          let y = event.deltaY;
          // console.log(y)
          let Y = y * (-80);
          let pre = editor?.session.getScrollTop();
          // editor?.session.setScrollTop(pre + Y)
          let cur = pre + Y;
          editor?.session.setScrollTop(cur)
          console.log(pre,cur)
          if(pre > -10 && pre <10) {
            return true
          }
         
          return false;
          // console.log(y,pre,Y)
          // if (pre > -5 && pre < 5) {
          //   return true;
          // }
          // return false;

        }
        //字体放大缩小
        let y = event.deltaY;
        console.log('del _y ',y)
        let curFont = parseInt(editor.getFontSize())
        if(curFont+y<=12) {
          return false;
        }
        // if(y) {
        editor.setFontSize(curFont + y)

        // }
        console.log(editor.getFontSize())
        // if (y > 0) {
        //   _font_size++
        //   // setFontSize(_font_size + 1)
        // } else if (y < 0) {
        //   _font_size--;
        //   // setFontSize(_font_size - 1)
        // }
        // if (y) {

        //   editor.setFontSize(_font_size);
        //   setFontSize(_font_size)

        // }

        return false;


      });

    }
    handle()
    // document.body.addEventListener('DOMMouseScroll',handle)
    // return ()=> document.body.removeEventListener(handle)
  }, [])
  return (
    <div className="row">
      <div className={`col-md-2  ${firstPage.fullH}`}>
        <button className="btn btn-dark">添加脚本</button>
        <button className="btn btn-info m-lg-1" >运行</button>
        <hr />
        <Nav defaultActiveKey="/" className={["flex-column", firstPage.app]}>
          {/* <Nav.Link href="/home">Active</Nav.Link>
          <Nav.Link eventKey="link-1">Link</Nav.Link>
          <Nav.Link eventKey="link-2">Link</Nav.Link> */}
          {
            scriptList.map((item, i) => {
              return (
                <Nav.Link 
                key={item.scriptId}
                eventKey={`${item.scriptId}`} > {item.name} </Nav.Link>
              )
            })
          }
        </Nav>
      </div>
      <div className={`col-md-10  ${firstPage.fullH} `} >
        <div className="card">
          <div className="card-header">
            <button className="btn btn-info">运行</button>
            <button className="btn btn-success m-lg-1">测试</button>
          </div>
          <div className={`card-body  ${firstPage.fullH}`}>
            <AceEditor
              ref={ref}
              className="_current_ace"
              style={{
                width: "100%",
                minHeight: "80vh",
                height: "auto"
              }}
              mode="javascript"
              theme="monokai"
              onChange={onChange}
              fontSize={18}
              name="UNIQUE_ID_OF_DIV"
              editorProps={{ $blockScrolling: true }}
            />
          </div>
        </div>
      </div>
    </div>
  )
}

react 绑定鼠标事件参考