Просмотр исходного кода

Merge pull request #13135 from overleaf/mj-empty-optional-argument-figure-modal

[cm6] Allow empty and missing optional arguments for figure modal

GitOrigin-RevId: 64e1310de21bdb3ff7ba244690cc83489bbfed40
Mathias Jakobsen 3 лет назад
Родитель
Сommit
5c95c907ec

+ 1 - 1
services/web/frontend/js/features/source-editor/components/figure-modal/figure-modal-context.tsx

@@ -16,7 +16,7 @@ type FigureModalState = {
   helpShown: boolean
   sourcePickerShown: boolean
   getPath?: () => Promise<string>
-  width: number
+  width: number | undefined
   includeCaption: boolean
   includeLabel: boolean
   error?: string

+ 1 - 1
services/web/frontend/js/features/source-editor/components/figure-modal/figure-modal-options.tsx

@@ -71,7 +71,7 @@ export const FigureModalFigureOptions: FC = () => {
           <Switcher
             name="figure-width"
             onChange={value => dispatch({ width: parseFloat(value) })}
-            defaultValue={width === 1 ? '1.0' : width.toString()}
+            defaultValue={width === 1 ? '1.0' : width?.toString()}
             disabled={hasComplexGraphicsArgument}
           >
             <SwitcherItem value="0.25" label={t('1_4_width')} />

+ 8 - 6
services/web/frontend/js/features/source-editor/components/figure-modal/figure-modal.tsx

@@ -97,12 +97,12 @@ const FigureModalContent = () => {
       }
       updateExistingFigure({
         name: figure.file.path,
-        hasComplexGraphicsArgument:
-          figure.unknownGraphicsArguments !== undefined,
+        // The empty string should *not* be a complex argument
+        hasComplexGraphicsArgument: Boolean(figure.unknownGraphicsArguments),
       })
       dispatch({
         source: FigureModalSource.EDIT_FIGURE,
-        width: figure.width ?? 0.5,
+        width: figure.width,
         includeCaption: figure.caption !== null,
         includeLabel: figure.label !== null,
       })
@@ -166,9 +166,9 @@ const FigureModalContent = () => {
           insert: '',
         })
       }
-      if (!figure.unknownGraphicsArguments) {
+      if (!figure.unknownGraphicsArguments && width) {
         // We understood the arguments, and should update the width
-        if (figure.graphicsCommandArguments) {
+        if (figure.graphicsCommandArguments !== null) {
           changes.push({
             from: figure.graphicsCommandArguments.from,
             to: figure.graphicsCommandArguments.to,
@@ -191,7 +191,9 @@ const FigureModalContent = () => {
       view.dispatch(
         view.state.changeByRange(range => {
           const { pos, suffix } = ensureEmptyLine(view.state, range)
-          const graphicxCommand = `\\includegraphics[width=${width}\\linewidth]{${path}}`
+          const widthArgument =
+            width !== undefined ? `[width=${width}\\linewidth]` : ''
+          const graphicxCommand = `\\includegraphics${widthArgument}{${path}}`
           const changes: ChangeSpec = view.state.changes({
             insert: `\\begin{figure}\n\\centering\n${graphicxCommand}${captionCommand}${labelCommand}${
               labelCommand || captionCommand ? '\n' : '' // Add an extra newline if we've added a caption or label

+ 1 - 5
services/web/frontend/js/features/source-editor/utils/tree-operations/environments.ts

@@ -337,11 +337,7 @@ export function parseFigureData(
   if (error) {
     return null
   }
-  if (
-    graphicsCommand === undefined ||
-    file === undefined ||
-    (width === undefined && unknownGraphicsArguments === undefined)
-  ) {
+  if (graphicsCommand === undefined || file === undefined) {
     return null
   }
   return new FigureData({