Reveal.js幻灯片不显示

时间:2018-01-25 09:45:51

标签: reactjs reveal.js highlight.js

我有Reveal.js的问题。当我第一次加载幻灯片集时,没有问题,第一次和下面的幻灯片放映。但是当我关闭我的幻灯片并再次来到这个集合或另一个集合时,没有幻灯片显示。我使用React js,这是我在揭密组件中的代码:

  /*
 * Npm import
 */
import React from 'react';
import PropTypes from 'prop-types';
import Reveal from 'reveal.js';
import { get } from 'axios';
import hljs from 'highlight.js';


/*
 * Local import
 */
import { settings, initialState } from './config';

// Stupid old schoold JS, but fuck.
window.Reveal = Reveal;


/*
 * Component
 */
export default class ReactReveal extends React.Component {
  /*
   * PropTypes
   */
  static propTypes = {
    // CSS
    id: PropTypes.string,
    className: PropTypes.string,

    // Redux
    url: PropTypes.string.isRequired,
    state: PropTypes.object,
    synced: PropTypes.bool,
    notify: PropTypes.bool,
    actions: PropTypes.objectOf(PropTypes.func.isRequired),
    active: PropTypes.bool.isRequired,
  }


  /*
   * State
   */
  state = {
    loaded: false,
  }


  /*
   * Lifecycles
   */
  componentWillMount() {
    this.load(this.props.url);
  }

  componentWillReceiveProps(nextProps) {
    const { url, state, synced } = nextProps;

    // New `url` prop => load url
    if (this.props.url !== url) {
      this.load(url);
      return;
    }

    // Update Reveal state if we are synced
    if (synced && state) {
      Reveal.setState(state);
    }
  }

  componentDidUpdate(prevProps, prevState) {
    // A deck has been loaded => init Reveal
    if (this.state.loaded && !prevState.loaded) {
      Reveal.initialize(settings);
      Reveal.setState(initialState);
      Reveal.addEventListener('slidechanged', this.notify);
      Reveal.addEventListener('fragmentshown', this.notify);
      Reveal.addEventListener('fragmenthidden', this.notify);
      Reveal.addEventListener('ready', this.highlight);
    }
    // Fix #366
    if (!prevProps.active && this.props.active) {
      window.dispatchEvent(new Event('resize'));
    }
  }

  componentWillUnmount() {
    if (this.state.loaded) {
      Reveal.removeEventListener('slidechanged', this.notify);
      Reveal.removeEventListener('fragmentshown', this.notify);
      Reveal.removeEventListener('fragmenthidden', this.notify);
      Reveal.removeEventListener('ready', this.highlight);
    }
  }


  /*
   * Actions
   */
  load = (url) => {
    get(url)
      .then(this.mount)
      .catch(this.mountError);
  }

  mount = ({ data }) => {
    // Inject Reveal slides
    this.innerHtml = { __html: data };

    // Update component
    this.setState({ loaded: true, error: false }, () => {
      // Update Reveal state if we are synced
      const { state, synced } = this.props;
      if (synced && state) {
        Reveal.setState(state);
      }
    });
  }

  mountError = (error) => {
    console.error(error);
    this.setState({
      loaded: false,
      error: true,
    });
  }


  /*
   * Handlers
   */
  notify = () => {
    const { actions, notify } = this.props;
    if (notify) {
      const state = Reveal.getState();
      actions.notifySlide(state);
    }
  }

  highlight() {
    hljs.initHighlighting.called = false;
    hljs.initHighlighting();
  }


  /*
   * Render
   */
  render() {
    // Vars
    const { id, className } = this.props;
    const attrs = {};

    // Id;
    if (id) {
      attrs.id = id;
    }

    // Class
    const classNames = ['reveal'];
    if (className) {
      classNames.push(className);
    }
    attrs.className = classNames.join(' ');
    console.log(attrs);
    return (
      <div {...attrs}>
        {this.state.loaded &&
          <div
            className="slides"
            dangerouslySetInnerHTML={this.innerHtml}
          />
        }
        {this.state.error &&
          <div className="utils-flexcenter">
            <div className="utils-flexcenter-child">
              <div>Error: The requested slides failed to load.</div>
              <div>URL: {this.props.url}</div>
            </div>
          </div>
        }
      </div>
    );
  }
}

似乎在我的第二次尝试中,揭示风格不适用。也不应用presentfuture类。 initialStatesettings是Reveal.js preconizes。

export const initialState = {
indexh: 0,
  indexv: 0,
  indexf: 0,
  paused: false,
  overview: false,
};
const Settings = {
  progress: true,
  center: true,
  slideNumber: true,
  embedded: true,
  width: 900,
  height: '100%',
  minScale: 0.7,
  maxScale: 1,
  transition: 'slide',
  controls: true,
  overview: true,
  help: true,
};

你知道为什么吗?

0 个答案:

没有答案