source-editor.stories.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import SourceEditor from '../../js/features/source-editor/components/source-editor'
  2. import { ScopeDecorator } from '../decorators/scope'
  3. import { useScope } from '../hooks/use-scope'
  4. import { useMeta } from '../hooks/use-meta'
  5. import { FC } from 'react'
  6. import { FileTreePathContext } from '@/features/file-tree/contexts/file-tree-path'
  7. import RangesTracker from '@overleaf/ranges-tracker'
  8. const FileTreePathProvider: FC<React.PropsWithChildren> = ({ children }) => (
  9. <FileTreePathContext.Provider
  10. value={{
  11. dirname: () => null,
  12. findEntityByPath: () => null,
  13. pathInFolder: () => null,
  14. previewByPath: (path: string) =>
  15. path === 'frog.jpg'
  16. ? {
  17. extension: 'png',
  18. url: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAAAB5fY51AAABhWlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9TpaJVETuIOGSogmBBVMRRq1CECqFWaNXB5NIvaNKQpLg4Cq4FBz8Wqw4uzro6uAqC4AeIq4uToouU+L+k0CLWg+N+vLv3uHsHCNUi06y2cUDTbTMRi4qp9KoYeIWAPnShB6Mys4w5SYqj5fi6h4+vdxGe1frcn6NbzVgM8InEs8wwbeIN4ulN2+C8TxxieVklPiceM+mCxI9cVzx+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UgWIBgAQgWgGABggUgWACCBQgWgGABCBYgWACCBSBYgGABCBaAYAGCBfCFVMtphUBKvYwQSBsPI3hYAIIFCBaAYAEIFiBYAIIFIFiAYAEIFoBgAYIFIFgAggUIFoBgAQgWIFgAggUIFoBgAQgWIFgAggUgWIBgAQgWgGABggUgWACCBQgWgGABCBYgWACCBSBYgGABCBaAYAGCBSBYAIIFCBaAYAEIFiBYAIIFCBaAYAEIFiBYAIIFIFiAYAEIFoBgAYIFIFgAggUIFoBgAQgWIFgAggUgWIBgAQgWgGABggUgWACCBQgWgGABCBYgWACCBQgWgGABCBYgWACCBSBYgGABCBaAYAGCBSBYAIIFCBaAYAEIFiBYAIIFIFiAYAEIFoBgAYIF8IUXjtUMuBMh1xAAAAAASUVORK5CYII=',
  19. }
  20. : null,
  21. }}
  22. >
  23. {children}
  24. </FileTreePathContext.Provider>
  25. )
  26. export default {
  27. title: 'Editor / Source Editor',
  28. component: SourceEditor,
  29. decorators: [
  30. (Story: any) =>
  31. ScopeDecorator(Story, {
  32. mockCompileOnLoad: true,
  33. providers: { FileTreePathProvider },
  34. }),
  35. (Story: any) => (
  36. <div style={{ height: '90vh' }}>
  37. <Story />
  38. </div>
  39. ),
  40. ],
  41. }
  42. const settings = {
  43. fontSize: 12,
  44. fontFamily: 'monaco',
  45. lineHeight: 'normal',
  46. editorTheme: 'textmate',
  47. overallTheme: '',
  48. mode: 'default',
  49. autoComplete: true,
  50. autoPairDelimiters: true,
  51. trackChanges: true,
  52. syntaxValidation: false,
  53. }
  54. const permissions = {
  55. write: true,
  56. }
  57. export const Latex = (args: any, { globals: { theme } }: any) => {
  58. // FIXME: useScope has no effect
  59. useScope({
  60. editor: {
  61. sharejs_doc: mockDoc(content.tex, changes.tex),
  62. open_doc_name: 'example.tex',
  63. },
  64. rootFolder: {
  65. name: 'rootFolder',
  66. id: 'root-folder-id',
  67. type: 'folder',
  68. children: [
  69. {
  70. name: 'example.tex.tex',
  71. id: 'example-doc-id',
  72. type: 'doc',
  73. selected: false,
  74. $$hashKey: 'object:89',
  75. },
  76. {
  77. name: 'frog.jpg',
  78. id: 'frog-image-id',
  79. type: 'file',
  80. linkedFileData: null,
  81. created: '2023-05-04T16:11:04.352Z',
  82. $$hashKey: 'object:108',
  83. },
  84. ],
  85. selected: false,
  86. },
  87. settings: {
  88. ...settings,
  89. overallTheme: theme === 'default-' ? '' : theme,
  90. },
  91. permissions,
  92. })
  93. useMeta({
  94. 'ol-showSymbolPalette': true,
  95. })
  96. return <SourceEditor />
  97. }
  98. export const Markdown = (args: any, { globals: { theme } }: any) => {
  99. useScope({
  100. editor: {
  101. sharejs_doc: mockDoc(content.md, changes.md),
  102. open_doc_name: 'example.md',
  103. },
  104. settings: {
  105. ...settings,
  106. overallTheme: theme === 'default-' ? '' : theme,
  107. },
  108. permissions,
  109. })
  110. return <SourceEditor />
  111. }
  112. export const Visual = (args: any, { globals: { theme } }: any) => {
  113. useScope({
  114. editor: {
  115. sharejs_doc: mockDoc(content.tex, changes.tex),
  116. open_doc_name: 'example.tex',
  117. showVisual: true,
  118. },
  119. settings: {
  120. ...settings,
  121. overallTheme: theme === 'default-' ? '' : theme,
  122. },
  123. permissions,
  124. })
  125. useMeta({
  126. 'ol-showSymbolPalette': true,
  127. 'ol-mathJaxPath': 'https://unpkg.com/mathjax@3.2.2/es5/tex-svg-full.js',
  128. 'ol-project_id': '63e21c07946dd8c76505f85a',
  129. })
  130. return <SourceEditor />
  131. }
  132. export const Bibtex = (args: any, { globals: { theme } }: any) => {
  133. useScope({
  134. editor: {
  135. sharejs_doc: mockDoc(content.bib, changes.bib),
  136. open_doc_name: 'example.bib',
  137. },
  138. settings: {
  139. ...settings,
  140. overallTheme: theme === 'default-' ? '' : theme,
  141. },
  142. permissions,
  143. })
  144. return <SourceEditor />
  145. }
  146. const MAX_DOC_LENGTH = 2 * 1024 * 1024 // ol-maxDocLength
  147. const mockDoc = (content: string, changes: Array<Record<string, any>> = []) => {
  148. const mockShareJSDoc = {
  149. getText() {
  150. return content
  151. },
  152. on() {
  153. // do nothing
  154. },
  155. insert() {
  156. // do nothing
  157. },
  158. del() {
  159. // do nothing
  160. },
  161. emit: (...args: any[]) => {
  162. console.log(...args)
  163. },
  164. }
  165. return {
  166. doc_id: 'story-doc',
  167. getSnapshot: () => {
  168. return content
  169. },
  170. attachToCM6: (cm6: any) => {
  171. cm6.attachShareJs(mockShareJSDoc, MAX_DOC_LENGTH)
  172. },
  173. detachFromCM6: () => {
  174. // Do nothing
  175. },
  176. on: () => {
  177. // Do nothing
  178. },
  179. off: () => {
  180. // Do nothing
  181. },
  182. setTrackChangesIdSeeds: () => {
  183. // Do nothing
  184. },
  185. setTrackChangesUserId: () => {
  186. // Do nothing
  187. },
  188. getTrackingChanges: () => {
  189. return true
  190. },
  191. getInflightOp: () => {
  192. return null
  193. },
  194. getPendingOp: () => {
  195. return null
  196. },
  197. ranges: new RangesTracker(changes, []),
  198. hasBufferedOps: () => false,
  199. }
  200. }
  201. const changes: Record<string, Array<Record<string, any>>> = {
  202. tex: [
  203. {
  204. id: '1',
  205. op: {
  206. i: 'Your introduction goes here! Simply start writing your document and use the Recompile button to view the updated PDF preview. Examples of commonly used commands and features are listed below, to help you get started.',
  207. p: 583,
  208. },
  209. meta: {
  210. user_id: '1',
  211. ts: new Date().toString(),
  212. },
  213. },
  214. ],
  215. md: [],
  216. bib: [],
  217. }
  218. const content = {
  219. tex: `\\documentclass{article}
  220. % Language setting
  221. % Replace \`english' with e.g. \`spanish' to change the document language
  222. \\usepackage[english]{babel}
  223. % Set page size and margins
  224. % Replace \`letterpaper' with \`a4paper' for UK/EU standard size
  225. \\usepackage[letterpaper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
  226. % Useful packages
  227. \\usepackage{amsmath}
  228. \\usepackage{graphicx}
  229. \\usepackage[colorlinks=true, allcolors=blue]{hyperref}
  230. \\title{Your Paper}
  231. \\author{You}
  232. \\begin{document}
  233. \\maketitle
  234. \\begin{abstract}
  235. Your abstract.
  236. \\end{abstract}
  237. \\section{Introduction}
  238. Your introduction goes here! Simply start writing your document and use the Recompile button to view the updated PDF preview. Examples of commonly used commands and features are listed below, to help you get started.
  239. Once you're familiar with the editor, you can find various project settings in the Overleaf menu, accessed via the button in the very top left of the editor. To view tutorials, user guides, and further documentation, please visit our \\href{https://www.overleaf.com/learn}{help library}, or head to our plans page to \\href{https://www.overleaf.com/user/subscription/plans}{choose your plan}.
  240. \\section{Some examples to get started}
  241. \\subsection{How to create Sections and Subsections}
  242. Simply use the section and subsection commands, as in this example document! With Overleaf, all the formatting and numbering is handled automatically according to the template you've chosen. If you're using Rich Text mode, you can also create new section and subsections via the buttons in the editor toolbar.
  243. \\subsection{How to include Figures}
  244. First you have to upload the image file from your computer using the upload link in the file-tree menu. Then use the includegraphics command to include it in your document. Use the figure environment and the caption command to add a number and a caption to your figure. See the code for Figure \\ref{fig:frog} in this section for an example.
  245. Note that your figure will automatically be placed in the most appropriate place for it, given the surrounding text and taking into account other figures or tables that may be close by. You can find out more about adding images to your documents in this help article on \\href{https://www.overleaf.com/learn/how-to/Including_images_on_Overleaf}{including images on Overleaf}.
  246. \\begin{figure}
  247. \\centering
  248. \\includegraphics[width=0.25\\linewidth]{frog.jpg}
  249. \\caption{This frog was uploaded via the file-tree menu.}\\label{fig:frog}
  250. \\end{figure}
  251. \\subsection{How to add Tables}
  252. Use the table and tabular environments for basic tables --- see Table~\\ref{tab:widgets}, for example. For more information, please see this help article on \\href{https://www.overleaf.com/learn/latex/tables}{tables}.
  253. \\begin{table}
  254. \\centering
  255. \\begin{tabular}{l|r}
  256. Item & Quantity \\\\\\hline
  257. Widgets & 42 \\\\
  258. Gadgets & 13
  259. \\end{tabular}
  260. \\caption{\\label{tab:widgets}An example table.}
  261. \\end{table}
  262. \\subsection{How to add Comments and Track Changes}
  263. Comments can be added to your project by highlighting some text and clicking \`\`Add comment'' in the top right of the editor pane. To view existing comments, click on the Review menu in the toolbar above. To reply to a comment, click on the Reply button in the lower right corner of the comment. You can close the Review pane by clicking its name on the toolbar when you're done reviewing for the time being.
  264. Track changes are available on all our \\href{https://www.overleaf.com/user/subscription/plans}{premium plans}, and can be toggled on or off using the option at the top of the Review pane. Track changes allow you to keep track of every change made to the document, along with the person making the change.
  265. \\subsection{How to add Lists}
  266. You can make lists with automatic numbering \\dots
  267. \\begin{enumerate}
  268. \\item Like this,
  269. \\item and like this.
  270. \\end{enumerate}
  271. \\dots or bullet points \\dots
  272. \\begin{itemize}
  273. \\item Like this,
  274. \\item and like this.
  275. \\end{itemize}
  276. \\subsection{How to write Mathematics}
  277. \\LaTeX{} is great at typesetting mathematics. Let $X_1, X_2, \\ldots, X_n$ be a sequence of independent and identically distributed random variables with $\\text{E}[X_i] = \\mu$ and $\\text{Var}[X_i] = \\sigma^2 < \\infty$, and let
  278. \\[S_n = \\frac{X_1 + X_2 + \\cdots + X_n}{n}
  279. = \\frac{1}{n}\\sum_{i}^{n} X_i\\]
  280. denote their mean. Then as $n$ approaches infinity, the random variables $\\sqrt{n}(S_n - \\mu)$ converge in distribution to a normal $\\mathcal{N}(0, \\sigma^2)$.
  281. \\subsection{How to change the margins and paper size}
  282. Usually the template you're using will have the page margins and paper size set correctly for that use-case. For example, if you're using a journal article template provided by the journal publisher, that template will be formatted according to their requirements. In these cases, it's best not to alter the margins directly.
  283. If however you're using a more general template, such as this one, and would like to alter the margins, a common way to do so is via the geometry package. You can find the geometry package loaded in the preamble at the top of this example file, and if you'd like to learn more about how to adjust the settings, please visit this help article on \\href{https://www.overleaf.com/learn/latex/page_size_and_margins}{page size and margins}.
  284. \\subsection{How to change the document language and spell check settings}
  285. Overleaf supports many different languages, including multiple different languages within one document.
  286. To configure the document language, simply edit the option provided to the babel package in the preamble at the top of this example project. To learn more about the different options, please visit this help article on \\href{https://www.overleaf.com/learn/latex/International_language_support}{international language support}.
  287. To change the spell check language, simply open the Overleaf menu at the top left of the editor window, scroll down to the spell check setting, and adjust accordingly.
  288. \\subsection{How to add Citations and a References List}
  289. You can simply upload a \\verb|.bib| file containing your BibTeX entries, created with a tool such as JabRef. You can then cite entries from it, like this: \\cite{greenwade93}. Just remember to specify a bibliography style, as well as the filename of the \\verb|.bib|. You can find a \\href{https://www.overleaf.com/help/97-how-to-include-a-bibliography-using-bibtex}{video tutorial here} to learn more about BibTeX.
  290. If you have an \\href{https://www.overleaf.com/user/subscription/plans}{upgraded account}, you can also import your Mendeley or Zotero library directly as a \\verb|.bib| file, via the upload menu in the file-tree.
  291. \\subsection{Good luck!}
  292. We hope you find Overleaf useful, and do take a look at our \\href{https://www.overleaf.com/learn}{help library} for more tutorials and user guides! Please also let us know if you have any feedback using the Contact Us link at the bottom of the Overleaf menu --- or use the contact form at \\url{https://www.overleaf.com/contact}.
  293. \\bibliographystyle{alpha}
  294. \\bibliography{sample}
  295. \\end{document}`,
  296. md: `# Heading
  297. This is **bold**
  298. This is _italic_`,
  299. bib: `@book{texbook,
  300. author = {Donald E. Knuth},
  301. year = {1986},
  302. title = {The {\\TeX} Book},
  303. publisher = {Addison-Wesley Professional}
  304. }
  305. @book{latex:companion,
  306. author = {Frank Mittelbach and Michel Gossens
  307. and Johannes Braams and David Carlisle
  308. and Chris Rowley},
  309. year = {2004},
  310. title = {The {\\LaTeX} Companion},
  311. publisher = {Addison-Wesley Professional},
  312. edition = {2}
  313. }
  314. @book{latex2e,
  315. author = {Leslie Lamport},
  316. year = {1994},
  317. title = {{\\LaTeX}: a Document Preparation System},
  318. publisher = {Addison Wesley},
  319. address = {Massachusetts},
  320. edition = {2}
  321. }
  322. @article{knuth:1984,
  323. title={Literate Programming},
  324. author={Donald E. Knuth},
  325. journal={The Computer Journal},
  326. volume={27},
  327. number={2},
  328. pages={97--111},
  329. year={1984},
  330. publisher={Oxford University Press}
  331. }
  332. @inproceedings{lesk:1977,
  333. title={Computer Typesetting of Technical Journals on {UNIX}},
  334. author={Michael Lesk and Brian Kernighan},
  335. booktitle={Proceedings of American Federation of
  336. Information Processing Societies: 1977
  337. National Computer Conference},
  338. pages={879--888},
  339. year={1977},
  340. address={Dallas, Texas}
  341. }
  342. `,
  343. }