diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6ab38c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.log diff --git a/main.go b/main.go index 877cd18..31181d8 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "net/http" "git.andreafazzi.eu/andrea/probo/hasher/sha256" - "git.andreafazzi.eu/andrea/probo/logger" "git.andreafazzi.eu/andrea/probo/store/memory" "github.com/sirupsen/logrus" ) @@ -13,7 +12,7 @@ import ( const port = "3000" func main() { - logger.SetLevel(logger.DebugLevel) + // logger.SetLevel(logger.DebugLevel) server := NewProboCollectorServer( memory.NewMemoryProboCollectorStore( diff --git a/misc/logseq/src/index.ts b/misc/logseq/src/index.ts index 86ed293..f8e2dc8 100644 --- a/misc/logseq/src/index.ts +++ b/misc/logseq/src/index.ts @@ -8,6 +8,8 @@ const uniqueIdentifier = () => .toString(36) .replace(/[^a-z]+/g, ""); +const sanitizeBlockContent = (text: string) => text.replace(/((?<=::).*|.*::)/g, "").replace(/{.*}/, "").trim() + async function fetchQuizzes() { const { status: status, content: quizzes } = await fetch(endpoint).then(res => res.json()) const ret = quizzes || [] @@ -17,6 +19,19 @@ async function fetchQuizzes() { }) } +const render = (id, slot, status: ("modified" | "saved" | "error")) => { + logseq.provideUI({ + key: `${id}`, + slot, + reset: true, + template: ` +${status === 'saved' ? 'saved' : ''} + +`, + }); + +} + const main = () => { console.log("logseq-probo-plugin LOADED!"); @@ -37,55 +52,60 @@ const main = () => { `{{renderer :probo_${uniqueIdentifier()}}}` ); - // const currBlock = await logseq.Editor.getCurrentBlock(); - // await logseq.Editor.insertBlock(currBlock.uuid, 'Write your question here...', - // { - // sibling: false, - // before: false, - // } - // ); - - // await logseq.Editor.exitEditingMode(); - }); logseq.App.onMacroRendererSlotted(async ({ slot, payload }) => { const [type] = payload.arguments; + + if (!type.startsWith(":probo")) return + const id = type.split("_")[1]?.trim(); const proboId = `probo_${id}`; + let status: ("modified" | "saved" | "error") + logseq.provideModel({ async createOrUpdateQuiz() { - const parentBlock = await logseq.Editor.getBlock(payload.uuid, { includeChildren: true }); - const answers = parentBlock.children.map((answer: BlockEntity, i: number) => { - return { text: answer.content, correct: (i == 0) ? true : false } - }) + const parentBlock = await logseq.Editor.getBlock(payload.uuid, { includeChildren: true }); + const answers = parentBlock.children.map((answer: BlockEntity, i: number) => { + return { text: answer.content, correct: (i == 0) ? true : false } + }) - const quiz = { - question: {text: parentBlock.content }, - answers: answers - } + const quiz = { + question: { text: sanitizeBlockContent(parentBlock.content) }, + answers: answers + } - const res = await fetch(endpoint+'/create', { method: 'POST', body: JSON.stringify(quiz) }) - const data = await res.json(); - await logseq.Editor.upsertBlockProperty(parentBlock.uuid, `probo-quiz-uuid`, data.content.ID) + if (parentBlock.properties.proboQuizUuid) { + const res = await fetch(endpoint + `/update/${parentBlock.properties.proboQuizUuid}`, { method: 'PUT', body: JSON.stringify(quiz) }) + const data = await res.json(); + await logseq.Editor.upsertBlockProperty(parentBlock.uuid, `probo-quiz-uuid`, data.content.ID) + render(proboId, slot, "saved") + } else { + const res = await fetch(endpoint + '/create', { method: 'POST', body: JSON.stringify(quiz) }) + const data = await res.json(); + await logseq.Editor.upsertBlockProperty(parentBlock.uuid, `probo-quiz-uuid`, data.content.ID) + render(proboId, slot, "saved") + } } }); logseq.provideStyle(` - .renderBtn { - border: 1px solid white; - border-radius: 8px; - padding: 5px; - font-size: 80%; - background-color: black; - color: white; - } - .renderBtn:hover { - background-color: white; - color: black; - } +.renderBtn { +border: 1px solid white; +border-radius: 8px; +padding: 5px; +margin-right: 5px; +font-size: 80%; +background-color: black; +color: white; +} + +.renderBtn:hover { +background-color: white; +color: black; +} `); logseq.provideUI({ diff --git a/store/memory/memory.go b/store/memory/memory.go index 7235490..7234afc 100644 --- a/store/memory/memory.go +++ b/store/memory/memory.go @@ -117,8 +117,8 @@ func (s *MemoryProboCollectorStore) createAnswerFromHash(hash string, answer *mo func (s *MemoryProboCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) { result := make([]*models.Quiz, 0) - for hash := range s.quizzesHashes { - result = append(result, s.getQuizFromHash(hash)) + for id := range s.quizzes { + result = append(result, s.getQuizFromID(id)) } return result, nil } @@ -163,7 +163,7 @@ func (s *MemoryProboCollectorStore) createOrUpdateQuiz(r *client.CreateUpdateQui }) } if answer.Correct { - quiz.Correct = a // s.readAnswer(answerID) + quiz.Correct = a } quiz.Answers = append(quiz.Answers, a) }