WaniKani Markdown Editor Notes (2023)

Write Markdown and HTML in the notes

目前为 2023-08-28 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name WaniKani Markdown Editor Notes (2023)
  3. // @namespace wanikani
  4. // @description Write Markdown and HTML in the notes
  5. // @version 2.1.2
  6. // @require https://uicdn.toast.com/editor/latest/toastui-editor-all.min.js
  7. // @require https://unpkg.com/dexie@3/dist/dexie.js
  8. // @require https://greasyfork.org/scripts/430565-wanikani-item-info-injector/code/WaniKani%20Item%20Info%20Injector.user.js?version=1241826
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=markdownguide.org
  10. // @match *://www.wanikani.com/*
  11. // @match *://preview.wanikani.com/*
  12. // @license MIT
  13. // @homepage https://greasyfork.org/en/scripts/468764-wanikani-markdown-editor-notes-2023
  14. // @source https://github.com/patarapolw/wanikani-userscript/blob/master/userscripts/markdown-notes.user.js
  15. // @supportURL https://community.wanikani.com/t/userscript-markdown-editor-notes-2023/62246
  16. // @grant none
  17. // ==/UserScript==
  18.  
  19. // @ts-check
  20. /// <reference path="./types/item-info.d.ts" />
  21. (function () {
  22. 'use strict';
  23.  
  24. const entryClazz = 'wk-markdown-notes';
  25.  
  26. // @ts-ignore
  27. const _Dexie = /** @type {typeof import('dexie').default} */ (Dexie);
  28. /**
  29. * @typedef {{ id: number; state: any; markdown: string }} EntryMarkdown
  30. */
  31.  
  32. class Database extends _Dexie {
  33. /** @type {import('dexie').Table<EntryMarkdown, number>} */
  34. markdown;
  35.  
  36. constructor() {
  37. super(entryClazz);
  38. this.version(1).stores({
  39. markdown: 'id',
  40. });
  41. }
  42. }
  43.  
  44. const db = new Database();
  45.  
  46. /** @type {HTMLElement} */
  47. let elEditor;
  48. /** @type {import('@toast-ui/editor').Editor} */
  49. let editor;
  50. /** @type {WKItemInfoState} */
  51. let state;
  52.  
  53. const injector = wkItemInfo
  54. .under('meaning,reading')
  55. .spoiling('nothing')
  56. .append('Markdown Notes', (o) => {
  57. if (editor) {
  58. save();
  59. }
  60.  
  61. state = o;
  62.  
  63. const onElLoaded = () => {
  64. db.markdown.get(state.id).then((entry) => {
  65. if (editor) {
  66. editor.setMarkdown(entry?.markdown || '');
  67. setTimeout(() => {
  68. editor.blur();
  69. });
  70. } else {
  71. /** @type {import('@toast-ui/editor').EditorOptions} */
  72. const opts = {
  73. el: elEditor,
  74. initialEditType: 'markdown',
  75. previewStyle: 'vertical',
  76. hideModeSwitch: true,
  77. linkAttributes: {
  78. target: '_blank',
  79. },
  80. previewHighlight: false,
  81. customHTMLSanitizer: (s) => {
  82. return s;
  83. },
  84. customHTMLRenderer: {
  85. text: function (node, ctx) {
  86. /** @type {import('@toast-ui/editor/types/toastmark').HTMLToken[]} */
  87. const out = [];
  88.  
  89. /**
  90. *
  91. * @param {string} tag
  92. * @param {() => void} doInside
  93. */
  94. const addTag = (tag, doInside) => {
  95. out.push({
  96. type: 'openTag',
  97. tagName: tag,
  98. });
  99.  
  100. doInside();
  101.  
  102. out.push({
  103. type: 'closeTag',
  104. tagName: tag,
  105. });
  106. };
  107.  
  108. /**
  109. *
  110. * @param {string} rb
  111. * @param {string} rt
  112. */
  113. const addRubyContent = (rb, rt) => {
  114. addTag('ruby', () => {
  115. out.push({
  116. type: 'text',
  117. content: rb,
  118. });
  119.  
  120. addTag('rp', () => {
  121. out.push({
  122. type: 'text',
  123. content: '(',
  124. });
  125. });
  126.  
  127. addTag('rt', () => {
  128. out.push({
  129. type: 'text',
  130. content: rt,
  131. });
  132. });
  133.  
  134. addTag('rp', () => {
  135. out.push({
  136. type: 'text',
  137. content: ')',
  138. });
  139. });
  140. });
  141. };
  142.  
  143. /** @param {string} s */
  144. const wkFuriganaParser = (s) => {
  145. const segments = s.split(/<(.+?)>\[(.+?)\]/g);
  146. while (segments.length) {
  147. const [raw, rb, rt] = segments.splice(0, 3);
  148. out.push({
  149. type: 'text',
  150. content: raw,
  151. });
  152.  
  153. if (rb) {
  154. addRubyContent(rb, rt);
  155. }
  156. }
  157. };
  158.  
  159. /** @param {string} s */
  160. const mdItFuriganaParser = (s) => {
  161. const segments = s.split(/\[(.+?)\]\{(.+?)\}/g);
  162. while (segments.length) {
  163. const [raw, rb, rt] = segments.splice(0, 3);
  164. wkFuriganaParser(raw);
  165.  
  166. if (rb) {
  167. addTag('ruby', () => {
  168. let rbArr = rb.split('.');
  169. if (rbArr.length === 1) {
  170. rbArr = rb.split('');
  171. }
  172. const rtArr = rt.split('.');
  173.  
  174. if (rbArr.length >= rtArr.length) {
  175. rtArr.map((t, i) => {
  176. if (i < rtArr.length - 1) {
  177. addRubyContent(rbArr[i], t);
  178. } else {
  179. addRubyContent(rbArr.slice(i).join(''), t);
  180. }
  181. });
  182. } else {
  183. addRubyContent(rb, rt);
  184. }
  185. });
  186. }
  187. }
  188. };
  189.  
  190. mdItFuriganaParser(node.literal || '');
  191.  
  192. return out;
  193. },
  194. },
  195. autofocus: false,
  196. initialValue: entry?.markdown,
  197. };
  198.  
  199. // @ts-ignore
  200. editor = new toastui.Editor(opts);
  201. // @ts-ignore
  202. window.wkMarkdownEditor = editor;
  203.  
  204. const elSave = document.createElement('button');
  205. elSave.type = 'button';
  206. elSave.className = 'fa fa-save save-button';
  207.  
  208. elSave.onclick = () => {
  209. elSave.classList.add(isClickedClass);
  210. save();
  211. setTimeout(() => {
  212. elSave.classList.remove(isClickedClass);
  213. }, 100);
  214. };
  215.  
  216. editor.insertToolbarItem(
  217. { groupIndex: -1, itemIndex: -1 },
  218. {
  219. name: 'Save',
  220. el: elSave,
  221. },
  222. );
  223.  
  224. editor.on('blur', () => {
  225. save();
  226. });
  227. }
  228. });
  229. };
  230.  
  231. if (!elEditor) {
  232. elEditor = document.createElement('div');
  233. elEditor.id = 'wk-markdown-editor';
  234. elEditor.lang = 'ja';
  235. elEditor.onkeydown = (ev) => {
  236. ev.stopImmediatePropagation();
  237. ev.stopPropagation();
  238. };
  239. }
  240.  
  241. onElLoaded();
  242.  
  243. return elEditor;
  244. });
  245.  
  246. window.addEventListener('willShowNextQuestion', () => {
  247. injector.renew();
  248. });
  249.  
  250. function save() {
  251. if (editor) {
  252. db.markdown.put(
  253. { id: state.id, state, markdown: editor.getMarkdown() },
  254. state.id,
  255. );
  256.  
  257. if (!elEditor) {
  258. editor.destroy();
  259. }
  260. }
  261. }
  262.  
  263. const isClickedClass = 'is-clicked';
  264.  
  265. (function add_css() {
  266. const style = document.createElement('style');
  267.  
  268. const K = '.toastui-editor-defaultUI';
  269.  
  270. style.append(
  271. document.createTextNode(/* css */ `
  272. @import url("https://uicdn.toast.com/editor/latest/toastui-editor.min.css");
  273.  
  274. ${K} {
  275. /* Font list from Jisho.org */
  276. --md-font-family-sans-serif: "Source Han Sans", "源ノ角ゴシック", "Hiragino Sans", "HiraKakuProN-W3", "Hiragino Kaku Gothic ProN W3", "Hiragino Kaku Gothic ProN", "ヒラギノ角ゴ ProN W3", "Noto Sans", "Noto Sans CJK JP", "メイリオ", Meiryo, "游ゴシック", YuGothic, "MS Pゴシック", "MS PGothic", "MS ゴシック", "MS Gothic", sans-serif;
  277. --md-font-family-serif: "HiraMinProN-W3", "Hiragino Mincho ProN W3", "Hiragino Mincho ProN", "ヒラギノ明朝 ProN W3", "游明朝", YuMincho, "HG明朝E", "MS P明朝", "MS PMincho", "MS 明朝", "MS Mincho", serif;
  278. --md-font-family: var(--md-font-family-sans-serif);
  279.  
  280. background-color: #fff;
  281. }
  282.  
  283. ${K} .ProseMirror {
  284. font-family: var(--md-font-family);
  285. }
  286.  
  287. ${K} .toastui-editor-md-preview * {
  288. font-family: var(--md-font-family);
  289. }
  290.  
  291. ${K} .serif {
  292. font-family: var(--md-font-family-serif);
  293. }
  294.  
  295. ${K} .sans,
  296. ${K} .sans-serif {
  297. font-family: var(--md-font-family-sans-serif);
  298. }
  299.  
  300. ${K} big {
  301. font-size: 1.7em;
  302. }
  303.  
  304. ${K} small {
  305. font-size: 0.7em;
  306. }
  307.  
  308. ${K} button.save-button {
  309. position: relative;
  310. background: transparent;
  311. font-size: 1em;
  312. bottom: 0.5em;
  313. }
  314.  
  315. ${K} button.save-button.${isClickedClass} {
  316. background-color: gray;
  317. }
  318. `),
  319. );
  320.  
  321. document.head.append(style);
  322. })();
  323. })();