Editor.tsx

import React from 'react';
import {Editor, ShortcutKey} from 'amis-editor';
import {inject, observer} from 'mobx-react';
import {RouteComponentProps} from 'react-router-dom';
import {toast, Select} from 'amis';
import {currentLocale} from 'i18n-runtime';
import {Icon} from '../icons/index';
import {IMainStore} from '../store';
import '../editor/DisabledEditorPlugin'; // 用于隐藏一些不需要的Editor预置组件
import '../renderer/MyRenderer';
import '../editor/MyRenderer';
import axios from 'axios'

let currentIndex = -1;

let host = `${window.location.protocol}//${window.location.host}`;

// 如果在 gh-pages 里面
if (/^\/amis-editor-demo/.test(window.location.pathname)) {
  host += '/amis-editor';
}

const schemaUrl = `${host}/schema.json`;

const editorLanguages = [
  {
    label: '简体中文',
    value: 'zh-CN'
  },
  {
    label: 'English',
    value: 'en-US'
  }
];

export default inject('store')(
  observer(function ({
    store,
    location,
    history,
    match
  }: {store: IMainStore} & RouteComponentProps<{id: string}>) {
    const index: number = parseInt(match.params.id, 10);
    const curLanguage = currentLocale(); // 获取当前语料类型

    // let value= {label: string; icon: string; path: string};
    // store.setAddPageIsOpen(true);
    // store.addPage({
    //   ...value,
    //   schema: {
    //     type: 'page',
    //     title: value.label,
    //     body: '这是你刚刚新增的页面。'
    //   }
    // });
    // store.setAddPageIsOpen(false);


    console.log(index);
    if (index !== currentIndex) {
      currentIndex = index;
      axios({
        method: 'get',
        url: '/api/AmisHandler.ashx?id='+index,
        headers: {
          'Content-Type': 'application/json'
        }
      })
      .then(function (response) {
          console.log(response.data);
          store.updateSchema(response.data);
      });
      //store.updateSchema(store.pages[index].schema);
    }

    function save() {
      axios({
        method: 'post',
        url: '/api/AmisPost.ashx',
        data: {
          id: index,
          schema: store.schema
        }
      });
      //store.updatePageSchemaAt(index);
      toast.success('保存成功', '提示');
    }

    function onChange(value: any) {
      store.updateSchema(value);
      //store.updatePageSchemaAt(index);
    }

    function changeLocale(value: string) {
      localStorage.setItem('suda-i18n-locale', value);
      window.location.reload();
    }

    function exit() {
      //history.push(`/${store.pages[index].path}`);
    }

    return (
     


       

         
amis 可视化编辑器

         

           

                              className={`Editor-view-mode-btn editor-header-icon ${
                  !store.isMobile ? 'is-active' : ''
                }`}
                onClick={() => {
                  store.setIsMobile(false);
                }}
              >
               
             

                              className={`Editor-view-mode-btn editor-header-icon ${
                  store.isMobile ? 'is-active' : ''
                }`}
                onClick={() => {
                  store.setIsMobile(true);
                }}
              >
               
             

           

         

         


           
                          className="margin-left-space"
              options={editorLanguages}
              value={curLanguage}
              clearable={false}
              onChange={(e: any) => changeLocale(e.value)}
            />
                          className={`header-action-btn m-1 ${
                store.preview ? 'primary' : ''
              }`}
              onClick={() => {
                store.setPreview(!store.preview);
              }}
            >
              {store.preview ? '编辑' : '预览'}
           

            {!store.preview && (
             

                保存
             

            )}
         

       

       

                      theme={'cxd'}
            preview={store.preview}
            isMobile={store.isMobile}
            value={store.schema}
            onChange={onChange}
            onPreview={() => {
              store.setPreview(true);
            }}
            onSave={save}
            className="is-fixed"
            $schemaUrl={schemaUrl}
            showCustomRenderersPanel={true}
            amisEnv={{
              fetcher: store.fetcher,
              notify: store.notify,
              alert: store.alert,
              copy: store.copy
            }}
          />
       

     

    );
  })
);

你可能感兴趣的:(javascript,前端,react.js)