0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ONLYOFFICE AppsのEmbed SDK・Plugins SDKの新サンプル8種:承認ワークフロー・サイドバーページ・すぐに使えるプラグイン

0
Posted at

ONLYOFFICE AppsのEmbed SDK・Plugins SDKの新サンプル

概要

ONLYOFFICE Appsの開発者向けドキュメントに、Embed SDKとPlugins SDKの新しいサンプルが8種類追加されました。この記事では、各サンプルの仕組みと動かし方をまとめます。

SDK 区分 サンプル
Embed SDK 応用サンプル ドキュメント承認ワークフロー(Document approval workflow)
Plugins SDK 応用サンプル サイドバーナビゲーションプラグイン(Article navigation plugin)
Plugins SDK すぐに使えるサンプル Book reader、CodeMirror、Convert to markdown、Image editor、URL、ZIP archives

SDKのパッケージ名(@onlyoffice/docspace-plugin-sdk)やURL、コード内のDocSpace.SDKには、旧名称のdocspaceが残っています。

前提条件

  • ONLYOFFICE Appsのポータル(管理者またはルーム管理者のアカウント)
  • サイドバーナビゲーションプラグイン:ONLYOFFICE Apps 4.0.0以降、Node.js、npm
  • 承認ワークフロー:HTMLを配信するWebサーバー(ファイルシステムから直接開くのは不可)

1. 承認ワークフローを動かす(Embed SDK)

作成者がドキュメントを提出すると、カスタムルームが作成され、Pending reviewタグが付きます。レビュアーにはレビュー権限が付与されます。承認するとレビュアーの権限が閲覧に変わり、タグがApprovedに、修正依頼をするとタグがChanges requestedに変わります。サンプルでは簡略化のため、両方の役割を1つのブラウザセッションで実行します。

1-1. 使用するAPI

処理 API
ONLYOFFICE Appsの埋め込み DocSpace.SDK.initManager(config)、イベントonAppReady
ルームとファイルの作成 createRoom()、createFile()
ステータス管理 createTag()、addTagsToRoom()、removeTagsFromRoom()
権限の変更 PUT /api/2.0/files/rooms/{roomId}/share
レビュアー一覧 GET /api/2.0/people

ルームの種類と権限は定数で定義されています。

const CUSTOM_ROOM = 5
const REVIEW_ACCESS = 5
const READ_ACCESS = 2

1-2. ドキュメントの提出処理

ルームを作成し、ドキュメントを追加して、最初のタグを付けます。

async function createApprovalRequest() {
  const title = document.getElementById("documentTitle").value
  if (!title) return alert("Please enter a document title")
  const button = document.getElementById("createRequestBtn")
  button.disabled = true

  const room = await docSpace.createRoom(`Approval: ${title}`, CUSTOM_ROOM)
  if (!room?.id) {
    button.disabled = false
    return alert(`Error creating room: ${room?.message ?? "unknown error"}`)
  }
  roomId = room.id

  try {
    const file = await docSpace.createFile(roomId, `${title}.docx`, "{PUBLIC_DOCX_ID}")
    // A portal without API_ERROR support resolves errors instead of rejecting
    if (!file?.id) throw new Error(file?.message ?? "unknown error")
    await docSpace.createTag("Pending review")
    await docSpace.addTagsToRoom(roomId, ["Pending review"])
  } catch (error) {
    console.error(error)
    button.disabled = false
    return alert(`Error preparing the room: ${error.message}`)
  }

  document.getElementById("createStep").classList.add("hidden")
  document.getElementById("reviewerStep").classList.remove("hidden")
}

API_ERRORに対応していないポータルではエラーがrejectされずにresolveされるため、room?.idとfile?.idで結果を確認しています。

1-3. 実行時の設定

  1. HTMLをWebサーバーから配信します。
  2. ONLYOFFICE AppsのDeveloper Toolsに、サーバーのルートURLを登録します。
  3. ONLYOFFICE Appsの管理者またはルーム管理者でログインします(タグを作成できるのはこのロールのみ)。

ルームはカスタムルーム(type 5)を使います。コラボレーションルーム(type 2)ではレビュー権限の付与がHTTP 403で拒否されます。また、サンプルは認証情報をクライアント側のコードに含めています。ローカルでのデモ用途に限り、本番環境ではバックエンドからトークンを取得してください。

2. サイドバーにページを追加する(Plugins SDK)

ArticleNavigationスコープを使い、サイドバーに2つの項目を追加するプラグインです。

  • Sample files:ファイルセクションに表示(全ユーザー)。ログイン中のユーザー名と、トーストを表示するボタン
  • Sample settings:設定セクションに表示(オーナーとフル管理者のみ)。トグルを切り替えるとトーストを表示

2-1. ナビゲーション項目の定義

プラグインクラスはIPluginとIArticleNavigationPluginを実装し、ポータルの準備ができたonLoadCallbackで項目を登録します。

import { Section, UserRole, type IArticleNavigationItem } from "@onlyoffice/docspace-plugin-sdk";

const filesItem: IArticleNavigationItem = {
  key: "article-navigation-sample-files",
  label: "Sample files",
  icon: "plugin-icon.svg",
  component: FilesPage,
  appears: [Section.Files],
};

const settingsItem: IArticleNavigationItem = {
  key: "article-navigation-sample-settings",
  label: "Sample settings",
  icon: "plugin-icon.svg",
  component: SettingsPage,
  appears: [Section.Settings],
  usersTypes: [UserRole.owner, UserRole.fullAdmin],
};

ページはポータルのReactツリー内で動くため、@onlyoffice/apps-ui-kitのコンポーネントでポータルのテーマがそのまま適用されます。ユーザー情報はuseCurrentUser、トースト表示はusePluginActionsのshowToastで取得します。サイドバーのページではuseCurrentFileはnullを返します。

2-2. ビルドとアップロード

npm i -g @onlyoffice/docspace-plugin-sdk
npx create-docspace-plugin
npm install @onlyoffice/apps-ui-kit
npm run build

npm run buildでdist/plugin.zipが生成されます。管理者がSettings→Integration→Pluginsからアップロードします。

UIキットは必ずパッケージのルートからimportします。@onlyoffice/apps-ui-kit/components/buttonのようなサブパスのimportは、ポータルが完全一致の指定子しか置き換えないため読み込みに失敗します。

3. すぐに使えるプラグインを有効にする

次の6つは完成済みのプラグインです。ONLYOFFICE Appsのクラウド版ではデフォルトで利用でき、それ以外の環境ではシステムプラグインとして有効にできます。すべてのプラグインがIPluginを実装しています。

プラグイン できること 主なインターフェース
Book reader .epub、.fb2、それらを含む.zipをリーダーで表示 IContextMenuPlugin、IFilePlugin
CodeMirror コードファイルをシンタックスハイライト付きで閲覧・編集 IApiPlugin、ISettingsPlugin、IMainButtonPluginほか
Convert to markdown .docx、.txt、.htmlを.mdに変換 IApiPlugin、IContextMenuPlugin
Image editor 画像のリサイズ・トリミング・回転・描画・テキスト追加 IApiPlugin、IContextMenuPlugin
URL 外部リンクを.urlファイルとして保存し、新しいタブで開く IMainButtonPlugin、IFilePluginほか
ZIP archives ZIPアーカイブのプレビュー・解凍・作成 IApiPlugin、IContextMenuPlugin、IFilePlugin

Convert to markdownは、Add watermarks to documentsが有効なルームのファイルを変換できません。

各プラグインのソースコードは次のリポジトリで公開されています。

参考リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?