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?

Wiki.jsのビジュアルエディタでGoogleスライドショーとかを埋め込めるようにした

Last updated at Posted at 2025-02-28

ニッチなWiki.jsの改造に関するお話です。
社内用の情報共有サイトとしてWiki.jsを導入しました。
非エンジニアの方たちが社内情報をまとめてくれているのですが、基本的にはビジュアルエディタで編集作業を行っています。
そこからGoogleスライドやスプレッドシートを埋め込みたい需要があるらしく、ちょろっと手を加えて改造しました。

実装

エディタ本体を改造するのは骨が折れるので、簡単目にJavaScriptで裏からViwer画面を埋め込んで行きます。
コードはテーマタブ内からheadに挿入するHTMLコードという欄の中にScriptタグを書き込んでいきます。

スクリーンショット 2025-02-28 14.22.24.png

中身は外部ファイル化してsrcで読み込ませてもいいですし、そのまま中に記載してもいいかと思います。
コードの中身は以下の通りです。

(() => {
  window.addEventListener('load', function () {
    const linkNodes = document.querySelectorAll('.v-main .contents a');
    linkNodes.forEach((linkNode, index) => {
      if (linkNode.classList.contains('is-external-link')) {
        // 外部リンク
        // 別タブで開くようにする
        linkNode.target = '_blank';
        linkNode.rel = 'noopener';

        // Googleスライドの公開リンクに対して表示機能を埋め込む
        if (linkNode.href.match(/presentation.*pub/)) {
          const insertContainer = document.createElement('div');
          const previewLink = linkNode.href.replace('pub?start', 'embed?start');
          insertContainer.innerHTML = `
            <iframe src="${previewLink}"
              width="960" height="569"
              frameborder="0"
              allowfullscreen="true"
              mozallowfullscreen="true"
              webkitallowfullscreen="true">
            </iframe>
          `;
          linkNode.parentNode.appendChild(insertContainer);
        }
        // Googleスプレッドシートの公開リンクに対して表示機能を埋め込む
        if (linkNode.href.match(/spreadsheets.*pub/)) {
          const insertContainer = document.createElement('div');
          const previewLink = linkNode.href.replace(/gid=0&single=true/, 'gid=0&amp;single=true&amp;widget=true&amp;headers=false');
          insertContainer.innerHTML = `<iframe src="${previewLink}" width="960" height="569"></iframe>`;
          linkNode.parentNode.appendChild(insertContainer);
        }
      } else {
        // 内部リンク
        // pdfのプレビュー機能を埋め込む
        if (linkNode.href.match(/\.view\.pdf/)) {
          const insertContainer = document.createElement('div');
          const previewLink = linkNode.href;
          insertContainer.innerHTML = `<embed-pdf src="${previewLink}"></embed-pdf>`;
          linkNode.parentNode.appendChild(insertContainer);
        }
      }
    });
  });
})();

ちなみにinnerHTMLはあまり良くないよねっていうのは分かった上で使っております。
(どーせ、社内Wikiなので)

稼働テスト

それではテストをしていきましょう。適当なGoogleスライドを作って公開設定をしていきます。

スクリーンショット 2025-02-28 14.50.44.png

設定ができたら、公開リンクをコピーして、

スクリーンショット 2025-02-28 14.51.15.png

こんな感じでビジュアルエディタから先ほどのリンクを挿入すると、

スクリーンショット 2025-02-28 14.48.38.png

ちゃんと埋め込めました。

スクリーンショット 2025-02-28 14.51.44.png

編集画面でもプレビューに表示されるようにと思ったのですが、イベント設定と管理が面倒なのでとりあえず閲覧時のみでViwerが挿入される状態に留めました。

ちなみに、PDFのプレビュー機能も導入したのですが、こちらは別口で書きたいと思います!

本日は以上です。需要があるかはわかりませんが、、、

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?