Etherpad/外掛

出自福留子孫
在2023年4月25日 (二) 16:11由丁志仁對話 | 貢獻所做的修訂版本

跳轉到: 導覽搜尋


可參考以下影片

寫出第一個Etherpad外掛

步驟

  • 可將 setting.json 中 "minify" 的設定由 true 改成 false ,方便 debug
  • 創造 plugins 資料夾(舉例 ep_previewimages)
  • 在資料夾中新增 packages.json ,裡面打此外掛的基本資訊(名字 作者 版本 描述 貢獻者 運行引擎),無涉外掛運用(範例(搜尋Package definition))
    其中 name 為外掛的名字, version 為外掛版本, description 為外掛介紹, author 為外掛作者, contributors 為貢獻者名單, dependencies 為寫程式所依賴的工具包, engines 為運行引擎。
{
  "name": "ep_PLUGINNAME",
  "version": "0.0.1",
  "description": "DESCRIPTION",
  "author": "USERNAME (REAL NAME) <MAIL@EXAMPLE.COM>",
  "contributors": [],
  "dependencies": {"MODULE": "0.3.20"},
  "engines": {"node": ">=12.13.0"}
}
以 ep_previewimages 中的 packages.json 為例:
{
  "name":"ep_previewimages",
  "description":"Image previewer, paste the URL of an image or upload an image using ep_fileupload",
  "version":"0.0.13",
  "author":"johnyma22 (John McLear) <john@mclear.co.uk>",
  "contributors":[],
  "dependencies":{},
  "repository":{"type":"git","url":"https://github.com/JohnMcLear/ep_previewimages.git"},
  "engines":{"node":"*"}
}
  • 在資料夾中新增 ep.json ,裡面打外掛的函式介紹(範例(搜尋Plugin definition))
    其中 name 為外掛名字, hook 及 client hook 中會寫出外掛使用的函式及位置。
{
  "parts":[
    {
      "name":"nameThisPartHoweverYouWant", 
      "hooks":{
        "authenticate":"ep_<plugin>/<file>:functionName1",         
        "expressCreateServer":ep_<plugin>/<file>:functionName2"
      },       
      "client_hooks":{"acePopulateDOMLine":"ep_<plugin>/<file>:functionName3"}
    }
  ]
}
以 ep_previewimages 中的 ep.json 為例:
{
  "parts":[
    {
      "name":"ep_previewimages",
      "client_hooks":{
        "aceGetFilterStack":"ep_previewimages/static/js/index:aceGetFilterStack",
        "aceCreateDomLine":"ep_previewimages/static/js/index:aceCreateDomLine"
      }
    }
  ]
}
  • 最後寫出 index.js 裡面使用 javascript 語法寫出所需要的函式。

可用函式解說:

在解說 index.js 之前,先介紹一些其中可以用的函式:

  1. exports 物件, Node.js 的原生物件,用來定義模組的公開介面,也就是透過「require」引入模組後,可以訪問的屬性或方法。
    如「exports.aceGetFilterStack = function(name, context){...} 」是 Etherpad ep_previewimages 外掛的的方法,載入此外掛後。就可以使用此方法來過濾出文本中匹配的行。
  2. context.linestylefilter.getRegexpFilter(第一參數,第二參數),造一個正則表達式過濾器,造好後添加到過濾器堆疊中,並在處理 Etherpad 文檔時使用。
    第一個參數:一個正則表達式物件,用於指定要在文本中匹配的模式。如:「new RegExp("http.+((\.[pP][nN][gG])|(\.[jJ][pP][gG])|(\.[gG][iI][fF])|(\.[jJ][pP][eE][gG])|(\.[bB][mM][pP])|(\.[sS][vV][gG]))","g")」
    第二個參數:一個字串,用於指定「與匹配到的文本相關聯」的樣式名。如:"image"。