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
This commit is contained in:
Mathias Jakobsen
2023-05-19 09:30:15 +01:00
committed by Copybot
parent 480672bf7a
commit 5c95c907ec
4 changed files with 11 additions and 13 deletions

View File

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

View File

@@ -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')} />

View File

@@ -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

View File

@@ -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({