7.打开对话框

创建对话框页面
1.在项目根目录下的 ./src 文件夹中,新建文件夹“dialogs”。
2.在 ./src/dialogs 文件夹中,新建文件“popup.html”,“popup.tsx”。
3.将下面的标记添加到 popup.html 中。 注意:

<!-- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. -->
<!-- See LICENSE in the project root for license information -->

<!doctype html>
<html lang="en" data-framework="typescript">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Dialog for My Office Add-in</title>

    <!-- Office JavaScript API -->
    <script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1.1/hosted/office.js"></script>

    <!-- For more information on Office UI Fabric, visit https://developer.microsoft.com/fabric. -->
    <link rel="stylesheet" href="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-core/9.6.1/css/fabric.min.css"/>

    <!-- Template styles -->
    
</head>

<body class="ms-font-m ms-Fabric">
    <div id="dia"></div>
</body>

</html>

将下面代码写入popup.tsx中:

import * as React from "react";
import * as ReactDOM from "react-dom";
import { useState } from 'react';


Office.onReady(() => {
    // If needed, Office.js is ready to be called
});



function Dial() {
    const [user, setUser]: any = useState(0);
    const handleChange = (e) => {
        let name = e.target.value;
        setUser(name)

    }
    const handleSubmit = () => {
        console.log({ user });
        Office.context.ui.messageParent(user);

    }

    return <div>
        <input onChange={handleChange} value={user}></input>
        <button onClick={handleSubmit}>submit</button>
    </div>
}



ReactDOM.render(
    <Dial />,
    document.getElementById("dia")
);

4.更新 webpack 配置设置
打开项目根目录中的 webpack.config.js 文件,并完成以下步骤。
1.在 config 对象内找到 entry 对象并为 popup 添加新条目。

popup: "./src/dialogs/popup.js"

完成此操作之后,新的 entry 对象将与此类似:

entry: {
      vendor: ["react", "react-dom", "core-js", "office-ui-fabric-react"],
      taskpane: ["react-hot-loader/patch", "./src/taskpane/index.tsx"],
      commands: "./src/commands/commands.ts",
      popup: "./src/dialogs/popup.tsx"
    },

2.在 config 对象中找到 plugins 数组,并添加下列对象至数组的结尾。

new HtmlWebpackPlugin({
        filename: "popup.html",
        template: "./src/dialogs/popup.html",
        chunks: ["polyfills", "popup"]
      }),

完成此操作之后,新的 plugins 数组将与此类似:

plugins: [
      new CleanWebpackPlugin(),
      new CopyWebpackPlugin({
        patterns: [
          {
            to: "taskpane.css",
            from: "./src/taskpane/taskpane.css"
          },
          {
            to: "[name]." + buildType + ".[ext]",
            from: "manifest*.xml",
            transform(content) {
              if (dev) {
                return content;
              } else {
                return content.toString().replace(new RegExp(urlDev, "g"), urlProd);
              }
            }
          }
        ]
      }),
      new ExtractTextPlugin("[name].[hash].css"),
      new HtmlWebpackPlugin({
        filename: "taskpane.html",
        template: "./src/taskpane/taskpane.html",
        chunks: ["taskpane", "vendor", "polyfills"]
      }),
      new HtmlWebpackPlugin({
        filename: "commands.html",
        template: "./src/commands/commands.html",
        chunks: ["commands"]
      }),
      new HtmlWebpackPlugin({
        filename: "popup.html",
        template: "./src/dialogs/popup.html",
        chunks: ["polyfills", "popup"]
      }),
      new webpack.ProvidePlugin({
        Promise: ["es6-promise", "Promise"]
      })
    ],

打开 ../taskpane/components/App.tsx文件,在render() return 中加入一个button及一个label

<br />
        <button onClick={this.handleOpen}>打开对话框</button>
        <br />
        <label id="user-name"></label><br /><br />

添加下列声明至文件结尾。 此变量用于保留父页面执行上下文中的对象,以用作对话框页面执行上下文的中间对象

var dialog = null;

增加一个handleOpen方法

handleOpen = () => {
    Office.context.ui.displayDialogAsync(
      'https://localhost:3000/popup.html',
      { height: 45, width: 55 },
      result => {
        dialog = result.value;
        dialog.addEventHandler(Office.EventType.DialogMessageReceived, arg => {
          document.getElementById("user-name").innerHTML = arg.message;
          dialog.close();
        });
      }
    )

  }

以上代码已经完成了整个对话框的使用,

原理如下:

1.先在taskpane中用button点击触发事件handleOpen,
2.handleOpen 事件利用Office.context.ui.displayDialogAsync方法 定义窗口的地址,长及宽,
利用message获取回调值,更新Label
3.在input框输入值,单击submit键,会调用Office.context.ui.messageParent()方法,将值回调,

官方文档:
https://docs.microsoft.com/zh-cn/office/dev/add-ins/develop/dialog-api-in-office-add-ins

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容