Repository navigation
add standalone insert citation dialog for hosts without an editor - #1126
Conversation
The Insert Citation dialog only used the ProseMirror document to read the YAML front matter, and the command applied the result with a transaction. Let the bibliography manager accept YAML blocks directly, split the bibliography writes out of ensureSourcesInBibliography, and export an InsertCitationDialog class (also from the panmirror bundle) that returns the citation ids and any bibliography file the host should declare, so plain-text hosts can offer the dialog. Addresses rstudio/rstudio#7876.
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
vezwork
left a comment
There was a problem hiding this comment.
Looks good with small comments
| // parse yaml blocks from their code (with or w/o enclosing ---) | ||
| export function parseYamlBlocks(yamlBlocks: string[]): YamlBlock[] { | ||
| return yamlBlocks.map(yamlText => { | ||
| const yamlCode = stripYamlDelimeters(yamlText); | ||
| return { yamlCode, yaml: parseYaml(yamlCode) }; | ||
| }); | ||
| } |
There was a problem hiding this comment.
| // parse yaml blocks from their code (with or w/o enclosing ---) | |
| export function parseYamlBlocks(yamlBlocks: string[]): YamlBlock[] { | |
| return yamlBlocks.map(yamlText => { | |
| const yamlCode = stripYamlDelimeters(yamlText); | |
| return { yamlCode, yaml: parseYaml(yamlCode) }; | |
| }); | |
| } | |
| export const parseYamlBlocks(yamlBlocks: string[]): YamlBlock[] => yamlBlocks.map(parseYamlText); | |
| // parse a yaml block from its code (with or w/o enclosing ---) | |
| const parseYamlText(yamlText: string): YamlBlock => { | |
| const yamlCode = stripYamlDelimeters(yamlText); | |
| return { yamlCode, yaml: parseYaml(yamlCode) }; | |
| }; |
nitpick readability suggestion
There was a problem hiding this comment.
Good call. Done in 78cc65f, though as a function declaration to match the rest of this file (the suggestion also dropped an =). parseYamlNodes now shares the same helper, so the strip-and-parse step lives in one place.
|
|
||
| if (!bibliographyFile.isProject) { | ||
| ensureBibliographyFileForDoc(tr, bibliographyFile.displayPath); | ||
| } |
There was a problem hiding this comment.
It moved rather than went away. The old code called ensureBibliographyFileForDoc from inside the per-source loop, once per source with an id. That loop now lives in writeSourcesToBibliography, which only does the server writes so the standalone dialog (no document, no transaction) can share it. The document edit is in the wrapper ensureSourcesInBibliography just above, at line 770: one call after the write succeeds, when the bibliography is not project-level and any source has an id. Since the function returns early once the document already declares the bibliography, calling it once after the loop gives the same result as calling it per source. The standalone host gets bibliographyFile back in the result instead and declares it in its own front matter.
|
Thanks! Let's merge! |
This makes the visual editor's Insert Citation dialog usable without an editor instance, so RStudio can offer it in its plain-text source editor (rstudio/rstudio#7876, a long-standing request).
The dialog itself never needed the ProseMirror document. It only read the YAML front matter to find
bibliography:,references:andzotero:.InsertCitationCommandthen applied the result with a transaction: the[@key]text and, when a new bibliography file was created, abibliography:line in the front matter. This PR separates those two seams from the rest.Changes
BibliographyManagerand its providers accept either a document or plain YAML blocks (YamlBlockSource). Existing callers keep passing the doc and are unchanged.ensureSourcesInBibliographyis split. The newwriteSourcesToBibliographydoes the server writes and the Better BibTeX warning, and the wrapper still addsbibliography:to the doc in the same transaction.showInsertCitationDialogtakes aYamlBlockSource. The unuseddocparameter is removed from the panel and the bibliography source panel.InsertCitationDialogclass (editor/editor-citation.ts) is exported fromeditorand from the panmirror bundle.show({ yaml, theme?, selectionKey? })returns{ citationIds, intextCitationStyle, bibliographyFile?, selectionKey? }. The host inserts the citation text and declaresbibliographyFilein its front matter when set.editorUIWithDefaultImagesis extracted fromEditor.create. This also corrects theomni_insertandcitationsmerges, which spread the wholeimagesobject instead of the nested one.editor-uipasses the full defaults, so its images are unchanged.ensureThemeapplies the default theme CSS when no editor has applied a theme yet, since the panel relies on thepm-*color classes.Verification
tsc --noEmitpasses forapps/panmirrorandapps/vscode-editor, and eslint passes on the changed files.Standalone dialog. I loaded the built bundle in headless Chromium with a stub host and server and drove the dialog:
bibliography:citationIds: ["smith2020"],bibliographyFile: "references.bib"refs.bibcitationIds: ["rivera2021"], nobibliographyFilerefs.bibnullVisual editor, before and after. In an RStudio development server, I inserted an R Package citation from the visual editor's Insert Citation dialog in a new
.mddocument. I ran it once with the current bundle and once with this branch's bundle served in its place:[@knitr]insertedbibliography: references.bibaddedreferences.bibhas the knitr entryNotes
apps/vscode/CHANGELOG.mdentry, because nothing changes for VS Code or Positron users. The new export is for RStudio. Happy to add one if you'd prefer.packages/editor-collabandpackages/editor-codemirrortype-checkeditor/srcwithout JSX enabled, so theirtscalready reports TS6142 for the.tsxmodules it reaches. The new file adds one more of the same, and neither package has atscscript or CI step.