index.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import "@logseq/libs";
  2. import { BlockEntity } from "@logseq/libs/dist/LSPlugin.user";
  3. const endpoint = 'http://localhost:3000/quizzes';
  4. const uniqueIdentifier = () =>
  5. Math.random()
  6. .toString(36)
  7. .replace(/[^a-z]+/g, "");
  8. async function fetchQuizzes() {
  9. const { status: status, content: quizzes } = await fetch(endpoint).then(res => res.json())
  10. const ret = quizzes || []
  11. return ret.map((quiz, i) => {
  12. return `${i + 1}. ${quiz.Question.Text}`
  13. })
  14. }
  15. const main = () => {
  16. console.log("logseq-quizhub-plugin LOADED!");
  17. logseq.Editor.registerSlashCommand("Get All Quizzes", async () => {
  18. const currBlock = await logseq.Editor.getCurrentBlock();
  19. let blocks = await fetchQuizzes()
  20. blocks = blocks.map((it: BlockEntity) => ({ content: it }))
  21. await logseq.Editor.insertBatchBlock(currBlock.uuid, blocks, {
  22. sibling: false
  23. })
  24. });
  25. logseq.Editor.registerSlashCommand("Insert a batch of quizzes", async () => {
  26. await logseq.Editor.insertAtEditingCursor(
  27. `{{renderer :quizhub_${uniqueIdentifier()}}}`
  28. );
  29. const currBlock = await logseq.Editor.getCurrentBlock();
  30. await logseq.Editor.insertBlock(currBlock.uuid, 'Text of the question here...',
  31. {
  32. sibling: false,
  33. before: false,
  34. }
  35. );
  36. await logseq.Editor.exitEditingMode();
  37. });
  38. logseq.App.onMacroRendererSlotted(async ({ slot, payload }) => {
  39. const [type] = payload.arguments;
  40. const id = type.split("_")[1]?.trim();
  41. const quizhubId = `quizhub_${id}`;
  42. logseq.provideModel({
  43. async postQuiz() {
  44. const parentBlock = await logseq.Editor.getBlock(payload.uuid, { includeChildren: true });
  45. const quizzes = parentBlock.children.map((child: BlockEntity) => {
  46. const question = { text: child.content }
  47. const answers = child.children.map((answer: BlockEntity, i: number) => {
  48. return { text: answer.content, correct: (i == 0) ? true : false }
  49. })
  50. return { question: question, answers: answers }
  51. });
  52. quizzes.forEach(async quiz => {
  53. const res = await fetch(endpoint, { method: 'POST', body: JSON.stringify(quiz) })
  54. const data = await res.json();
  55. console.log(data)
  56. })
  57. }
  58. });
  59. logseq.provideStyle(`
  60. .renderBtn {
  61. border: 1px solid black;
  62. border-radius: 8px;
  63. padding: 3px;
  64. font-size: 80%;
  65. background-color: white;
  66. color: black;
  67. }
  68. .renderBtn:hover {
  69. background-color: black;
  70. color: white;
  71. }
  72. `);
  73. logseq.provideUI({
  74. key: `${quizhubId}`,
  75. slot,
  76. reset: true,
  77. template: `<button data-on-click="postQuiz" class="renderBtn">Save</button>`,
  78. });
  79. });
  80. };
  81. logseq.ready(main).catch(console.error);