@contentful/field-editor-markdown 2.3.2 → 2.3.3-canary.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,7 +28,7 @@ const _MarkdownTextarea = require("./components/MarkdownTextarea/MarkdownTextare
28
28
  const _MarkdownToolbar = require("./components/MarkdownToolbar");
29
29
  const _CheatsheetModalDialog = require("./dialogs/CheatsheetModalDialog");
30
30
  const _MarkdownActions = require("./MarkdownActions");
31
- const _isMarkdownListItem = require("./utils/isMarkdownListItem");
31
+ const _indentation = require("./utils/indentation");
32
32
  function _interop_require_default(obj) {
33
33
  return obj && obj.__esModule ? obj : {
34
34
  default: obj
@@ -89,7 +89,8 @@ function MarkdownEditor(props) {
89
89
  const [selectedTab, setSelectedTab] = _react.useState('editor');
90
90
  const [editor, setEditor] = _react.useState(null);
91
91
  const [canUploadAssets, setCanUploadAssets] = _react.useState(false);
92
- const [isCurrentLineAListItem, setIsCurrentLineAListItem] = _react.useState(false);
92
+ const [canIndentLine, setCanIndentLine] = _react.useState(false);
93
+ const [canDedentLine, setCanDedentLine] = _react.useState(false);
93
94
  _react.useEffect(()=>{
94
95
  if (props.enableTab) {
95
96
  setSelectedTab(props.enableTab);
@@ -131,7 +132,8 @@ function MarkdownEditor(props) {
131
132
  editor
132
133
  ]);
133
134
  const isActionDisabled = editor === null || props.isDisabled || selectedTab !== 'editor';
134
- const isIndentationDisabled = isActionDisabled || !isCurrentLineAListItem;
135
+ const indentDisabled = isActionDisabled || !canIndentLine;
136
+ const dedentDisabled = isActionDisabled || !canDedentLine;
135
137
  const direction = props.sdk.locales.direction[props.sdk.field.locale] ?? 'ltr';
136
138
  const actions = _react.useMemo(()=>{
137
139
  return (0, _MarkdownActions.createMarkdownActions)({
@@ -158,7 +160,8 @@ function MarkdownEditor(props) {
158
160
  }), /*#__PURE__*/ _react.createElement(_MarkdownToolbar.MarkdownToolbar, {
159
161
  mode: "default",
160
162
  disabled: isActionDisabled,
161
- indentationDisabled: isIndentationDisabled,
163
+ indentDisabled: indentDisabled,
164
+ dedentDisabled: dedentDisabled,
162
165
  canUploadAssets: canUploadAssets,
163
166
  actions: actions
164
167
  }), /*#__PURE__*/ _react.createElement(_MarkdownTextarea.MarkdownTextarea, {
@@ -171,15 +174,18 @@ function MarkdownEditor(props) {
171
174
  editor.setContent(props.value ?? '');
172
175
  editor.setReadOnly(!!props.isDisabled);
173
176
  setEditor(editor);
177
+ const syncIndentState = ()=>{
178
+ const line = editor.getCurrentLine();
179
+ setCanIndentLine((0, _indentation.canIndent)(line));
180
+ setCanDedentLine((0, _indentation.canDedent)(line));
181
+ };
174
182
  editor.events.onChange((value)=>{
175
183
  const trimmedValue = value.replace(/^\s+$/gm, '');
176
184
  props.saveValueToSDK(trimmedValue);
177
185
  setCurrentValue(value);
178
- setIsCurrentLineAListItem((0, _isMarkdownListItem.isMarkdownListItem)(editor.getCurrentLine()));
179
- });
180
- editor.events.onCursorActivity(()=>{
181
- setIsCurrentLineAListItem((0, _isMarkdownListItem.isMarkdownListItem)(editor.getCurrentLine()));
186
+ syncIndentState();
182
187
  });
188
+ editor.events.onCursorActivity(syncIndentState);
183
189
  }
184
190
  }), selectedTab === 'preview' && /*#__PURE__*/ _react.createElement(_react.Suspense, {
185
191
  fallback: /*#__PURE__*/ _react.createElement(_MarkdownPreviewSkeleton.default, null)
@@ -12,6 +12,7 @@ const _codemirror = /*#__PURE__*/ _interop_require_default(require("codemirror")
12
12
  require("codemirror/addon/edit/matchbrackets");
13
13
  const _throttle = /*#__PURE__*/ _interop_require_default(require("lodash/throttle"));
14
14
  const _transform = /*#__PURE__*/ _interop_require_default(require("lodash/transform"));
15
+ const _indentation = require("../../utils/indentation");
15
16
  const _userAgent = /*#__PURE__*/ _interop_require_wildcard(require("../../utils/userAgent"));
16
17
  function _interop_require_default(obj) {
17
18
  return obj && obj.__esModule ? obj : {
@@ -100,7 +101,12 @@ function create(host, options) {
100
101
  }
101
102
  cm.setOption('extraKeys', {
102
103
  Tab: function() {
103
- replaceSelectedText(getIndentation());
104
+ const cursor = cm.getCursor();
105
+ const line = cm.getLine(cursor.line);
106
+ const indentUnit = cm.getOption('indentUnit') ?? 2;
107
+ if (cursor.ch > (0, _indentation.getLeadingSpaces)(line) || (0, _indentation.canIndent)(line, indentUnit)) {
108
+ replaceSelectedText(getIndentation());
109
+ }
104
110
  },
105
111
  Enter: 'newlineAndIndentContinueMarkdownList',
106
112
  Esc: ()=>{
@@ -13,6 +13,7 @@ const _min = /*#__PURE__*/ _interop_require_default(require("lodash/min"));
13
13
  const _range = /*#__PURE__*/ _interop_require_default(require("lodash/range"));
14
14
  const _times = /*#__PURE__*/ _interop_require_default(require("lodash/times"));
15
15
  const _repeat = /*#__PURE__*/ _interop_require_default(require("lodash/repeat"));
16
+ const _indentation = require("../../utils/indentation");
16
17
  function _interop_require_default(obj) {
17
18
  return obj && obj.__esModule ? obj : {
18
19
  default: obj
@@ -79,12 +80,17 @@ function create(editor) {
79
80
  editor.insertAtCursor(markup);
80
81
  }
81
82
  function indent() {
82
- editor.insertAtLineBeginning(editor.getIndentation());
83
+ const line = editor.getCurrentLine();
84
+ const indentUnit = editor.getIndentation().length;
85
+ if ((0, _indentation.canIndent)(line, indentUnit)) {
86
+ editor.insertAtLineBeginning(editor.getIndentation());
87
+ }
83
88
  }
84
89
  function dedent() {
85
- const indentation = editor.getIndentation();
86
- if (editor.lineStartsWith(indentation)) {
87
- editor.removeFromLineBeginning(indentation.length);
90
+ const line = editor.getCurrentLine();
91
+ if ((0, _indentation.canDedent)(line)) {
92
+ const remove = Math.min(editor.getIndentation().length, (0, _indentation.getLeadingSpaces)(line));
93
+ editor.removeFromLineBeginning(remove);
88
94
  }
89
95
  }
90
96
  function table(config) {
@@ -255,7 +255,7 @@ function AdditionalButtons(props) {
255
255
  "aria-label": "Horizontal rule",
256
256
  className: styles.icon
257
257
  })), /*#__PURE__*/ _react.createElement(ToolbarButton, {
258
- isDisabled: props.indentationDisabled,
258
+ isDisabled: props.indentDisabled,
259
259
  testId: "markdown-action-button-indent",
260
260
  tooltip: "Increase indentation",
261
261
  tooltipPlace: tooltipPlace,
@@ -264,7 +264,7 @@ function AdditionalButtons(props) {
264
264
  "aria-label": "Increase indentation",
265
265
  className: styles.icon
266
266
  })), /*#__PURE__*/ _react.createElement(ToolbarButton, {
267
- isDisabled: props.indentationDisabled,
267
+ isDisabled: props.dedentDisabled,
268
268
  testId: "markdown-action-button-dedent",
269
269
  tooltip: "Decrease indentation",
270
270
  tooltipPlace: tooltipPlace,
@@ -59,7 +59,8 @@ function _interop_require_wildcard(obj, nodeInterop) {
59
59
  const createProps = ()=>({
60
60
  canUploadAssets: true,
61
61
  disabled: false,
62
- indentationDisabled: false,
62
+ indentDisabled: false,
63
+ dedentDisabled: false,
63
64
  mode: 'default',
64
65
  actions: {
65
66
  headings: {
@@ -124,10 +125,10 @@ const createProps = ()=>({
124
125
  name: 'Undo'
125
126
  })).toBeInTheDocument();
126
127
  });
127
- (0, _vitest.it)('disables indentation controls when indentation is unavailable', async ()=>{
128
+ (0, _vitest.it)('disables indent button when indent is unavailable', async ()=>{
128
129
  const props = {
129
130
  ...createProps(),
130
- indentationDisabled: true
131
+ indentDisabled: true
131
132
  };
132
133
  (0, _react1.render)(/*#__PURE__*/ _react.createElement(_MarkdownToolbar.MarkdownToolbar, props));
133
134
  await _userevent.default.click(_react1.screen.getByRole('button', {
@@ -136,6 +137,22 @@ const createProps = ()=>({
136
137
  (0, _vitest.expect)(_react1.screen.getByRole('button', {
137
138
  name: 'Increase indentation'
138
139
  })).toBeDisabled();
140
+ (0, _vitest.expect)(_react1.screen.getByRole('button', {
141
+ name: 'Decrease indentation'
142
+ })).not.toBeDisabled();
143
+ });
144
+ (0, _vitest.it)('disables dedent button when dedent is unavailable', async ()=>{
145
+ const props = {
146
+ ...createProps(),
147
+ dedentDisabled: true
148
+ };
149
+ (0, _react1.render)(/*#__PURE__*/ _react.createElement(_MarkdownToolbar.MarkdownToolbar, props));
150
+ await _userevent.default.click(_react1.screen.getByRole('button', {
151
+ name: 'More actions'
152
+ }));
153
+ (0, _vitest.expect)(_react1.screen.getByRole('button', {
154
+ name: 'Increase indentation'
155
+ })).not.toBeDisabled();
139
156
  (0, _vitest.expect)(_react1.screen.getByRole('button', {
140
157
  name: 'Decrease indentation'
141
158
  })).toBeDisabled();
@@ -28,7 +28,7 @@ const _MarkdownToolbar = require("../components/MarkdownToolbar");
28
28
  const _CheatsheetModalDialog = require("../dialogs/CheatsheetModalDialog");
29
29
  const _MarkdownActions = require("../MarkdownActions");
30
30
  const _types = require("../types");
31
- const _isMarkdownListItem = require("../utils/isMarkdownListItem");
31
+ const _indentation = require("../utils/indentation");
32
32
  function _interop_require_default(obj) {
33
33
  return obj && obj.__esModule ? obj : {
34
34
  default: obj
@@ -140,7 +140,8 @@ const ZenModeModalDialog = (props)=>{
140
140
  const [currentValue, setCurrentValue] = _react.useState(props.initialValue ?? '');
141
141
  const [showPreview, setShowPreview] = _react.useState(true);
142
142
  const [editor, setEditor] = _react.useState(null);
143
- const [isCurrentLineAListItem, setIsCurrentLineAListItem] = _react.useState(false);
143
+ const [canIndentLine, setCanIndentLine] = _react.useState(false);
144
+ const [canDedentLine, setCanDedentLine] = _react.useState(false);
144
145
  _react.useEffect(()=>{
145
146
  props.sdk?.window?.updateHeight('100%');
146
147
  }, []);
@@ -176,7 +177,8 @@ const ZenModeModalDialog = (props)=>{
176
177
  }, /*#__PURE__*/ _react.createElement(_MarkdownToolbar.MarkdownToolbar, {
177
178
  mode: "zen",
178
179
  disabled: false,
179
- indentationDisabled: !isCurrentLineAListItem,
180
+ indentDisabled: !canIndentLine,
181
+ dedentDisabled: !canDedentLine,
180
182
  canUploadAssets: false,
181
183
  actions: actions
182
184
  })), /*#__PURE__*/ _react.createElement(_f36components.Grid.Item, {
@@ -193,14 +195,17 @@ const ZenModeModalDialog = (props)=>{
193
195
  editor.setReadOnly(false);
194
196
  setEditor(editor);
195
197
  editor.focus();
198
+ const syncIndentState = ()=>{
199
+ const line = editor.getCurrentLine();
200
+ setCanIndentLine((0, _indentation.canIndent)(line));
201
+ setCanDedentLine((0, _indentation.canDedent)(line));
202
+ };
196
203
  editor.events.onChange((value)=>{
197
204
  setCurrentValue(value);
198
205
  props.saveValueToSDK(value);
199
- setIsCurrentLineAListItem((0, _isMarkdownListItem.isMarkdownListItem)(editor.getCurrentLine()));
200
- });
201
- editor.events.onCursorActivity(()=>{
202
- setIsCurrentLineAListItem((0, _isMarkdownListItem.isMarkdownListItem)(editor.getCurrentLine()));
206
+ syncIndentState();
203
207
  });
208
+ editor.events.onCursorActivity(syncIndentState);
204
209
  }
205
210
  })), showPreview && /*#__PURE__*/ _react.createElement(_f36components.Grid.Item, {
206
211
  className: styles.previewSplit
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ MAX_PARAGRAPH_LEADING_SPACES: function() {
13
+ return MAX_PARAGRAPH_LEADING_SPACES;
14
+ },
15
+ canDedent: function() {
16
+ return canDedent;
17
+ },
18
+ canIndent: function() {
19
+ return canIndent;
20
+ },
21
+ getLeadingSpaces: function() {
22
+ return getLeadingSpaces;
23
+ }
24
+ });
25
+ const _isMarkdownListItem = require("./isMarkdownListItem");
26
+ const MAX_PARAGRAPH_LEADING_SPACES = 2;
27
+ function getLeadingSpaces(line) {
28
+ return line.match(/^ */)?.[0].length ?? 0;
29
+ }
30
+ function canIndent(line, indentUnit = 2) {
31
+ if ((0, _isMarkdownListItem.isMarkdownListItem)(line)) return true;
32
+ return getLeadingSpaces(line) + indentUnit <= MAX_PARAGRAPH_LEADING_SPACES;
33
+ }
34
+ function canDedent(line) {
35
+ return getLeadingSpaces(line) > 0;
36
+ }
@@ -10,7 +10,7 @@ import { MarkdownTextarea } from './components/MarkdownTextarea/MarkdownTextarea
10
10
  import { MarkdownToolbar } from './components/MarkdownToolbar';
11
11
  import { openCheatsheetModal } from './dialogs/CheatsheetModalDialog';
12
12
  import { createMarkdownActions } from './MarkdownActions';
13
- import { isMarkdownListItem } from './utils/isMarkdownListItem';
13
+ import { canIndent, canDedent } from './utils/indentation';
14
14
  const MarkdownPreview = /*#__PURE__*/ React.lazy(()=>import('./components/MarkdownPreview'));
15
15
  const styles = {
16
16
  container: css({
@@ -25,7 +25,8 @@ export function MarkdownEditor(props) {
25
25
  const [selectedTab, setSelectedTab] = React.useState('editor');
26
26
  const [editor, setEditor] = React.useState(null);
27
27
  const [canUploadAssets, setCanUploadAssets] = React.useState(false);
28
- const [isCurrentLineAListItem, setIsCurrentLineAListItem] = React.useState(false);
28
+ const [canIndentLine, setCanIndentLine] = React.useState(false);
29
+ const [canDedentLine, setCanDedentLine] = React.useState(false);
29
30
  React.useEffect(()=>{
30
31
  if (props.enableTab) {
31
32
  setSelectedTab(props.enableTab);
@@ -67,7 +68,8 @@ export function MarkdownEditor(props) {
67
68
  editor
68
69
  ]);
69
70
  const isActionDisabled = editor === null || props.isDisabled || selectedTab !== 'editor';
70
- const isIndentationDisabled = isActionDisabled || !isCurrentLineAListItem;
71
+ const indentDisabled = isActionDisabled || !canIndentLine;
72
+ const dedentDisabled = isActionDisabled || !canDedentLine;
71
73
  const direction = props.sdk.locales.direction[props.sdk.field.locale] ?? 'ltr';
72
74
  const actions = React.useMemo(()=>{
73
75
  return createMarkdownActions({
@@ -94,7 +96,8 @@ export function MarkdownEditor(props) {
94
96
  }), /*#__PURE__*/ React.createElement(MarkdownToolbar, {
95
97
  mode: "default",
96
98
  disabled: isActionDisabled,
97
- indentationDisabled: isIndentationDisabled,
99
+ indentDisabled: indentDisabled,
100
+ dedentDisabled: dedentDisabled,
98
101
  canUploadAssets: canUploadAssets,
99
102
  actions: actions
100
103
  }), /*#__PURE__*/ React.createElement(MarkdownTextarea, {
@@ -107,15 +110,18 @@ export function MarkdownEditor(props) {
107
110
  editor.setContent(props.value ?? '');
108
111
  editor.setReadOnly(!!props.isDisabled);
109
112
  setEditor(editor);
113
+ const syncIndentState = ()=>{
114
+ const line = editor.getCurrentLine();
115
+ setCanIndentLine(canIndent(line));
116
+ setCanDedentLine(canDedent(line));
117
+ };
110
118
  editor.events.onChange((value)=>{
111
119
  const trimmedValue = value.replace(/^\s+$/gm, '');
112
120
  props.saveValueToSDK(trimmedValue);
113
121
  setCurrentValue(value);
114
- setIsCurrentLineAListItem(isMarkdownListItem(editor.getCurrentLine()));
115
- });
116
- editor.events.onCursorActivity(()=>{
117
- setIsCurrentLineAListItem(isMarkdownListItem(editor.getCurrentLine()));
122
+ syncIndentState();
118
123
  });
124
+ editor.events.onCursorActivity(syncIndentState);
119
125
  }
120
126
  }), selectedTab === 'preview' && /*#__PURE__*/ React.createElement(React.Suspense, {
121
127
  fallback: /*#__PURE__*/ React.createElement(MarkdownPreviewSkeleton, null)
@@ -2,6 +2,7 @@ import CodeMirror from 'codemirror';
2
2
  import 'codemirror/addon/edit/matchbrackets';
3
3
  import throttle from 'lodash/throttle';
4
4
  import transform from 'lodash/transform';
5
+ import { canIndent, getLeadingSpaces } from '../../utils/indentation';
5
6
  import * as userAgent from '../../utils/userAgent';
6
7
  function stripUnit(value) {
7
8
  if (typeof value !== 'string') return value;
@@ -44,7 +45,12 @@ export function create(host, options) {
44
45
  }
45
46
  cm.setOption('extraKeys', {
46
47
  Tab: function() {
47
- replaceSelectedText(getIndentation());
48
+ const cursor = cm.getCursor();
49
+ const line = cm.getLine(cursor.line);
50
+ const indentUnit = cm.getOption('indentUnit') ?? 2;
51
+ if (cursor.ch > getLeadingSpaces(line) || canIndent(line, indentUnit)) {
52
+ replaceSelectedText(getIndentation());
53
+ }
48
54
  },
49
55
  Enter: 'newlineAndIndentContinueMarkdownList',
50
56
  Esc: ()=>{
@@ -3,6 +3,7 @@ import min from 'lodash/min';
3
3
  import range from 'lodash/range';
4
4
  import times from 'lodash/times';
5
5
  import repeat from 'lodash/repeat';
6
+ import { canIndent, canDedent, getLeadingSpaces } from '../../utils/indentation';
6
7
  function createPrefixToggleFn(prefix) {
7
8
  return (editor)=>{
8
9
  if (editor.lineStartsWith(prefix)) {
@@ -64,12 +65,17 @@ export function create(editor) {
64
65
  editor.insertAtCursor(markup);
65
66
  }
66
67
  function indent() {
67
- editor.insertAtLineBeginning(editor.getIndentation());
68
+ const line = editor.getCurrentLine();
69
+ const indentUnit = editor.getIndentation().length;
70
+ if (canIndent(line, indentUnit)) {
71
+ editor.insertAtLineBeginning(editor.getIndentation());
72
+ }
68
73
  }
69
74
  function dedent() {
70
- const indentation = editor.getIndentation();
71
- if (editor.lineStartsWith(indentation)) {
72
- editor.removeFromLineBeginning(indentation.length);
75
+ const line = editor.getCurrentLine();
76
+ if (canDedent(line)) {
77
+ const remove = Math.min(editor.getIndentation().length, getLeadingSpaces(line));
78
+ editor.removeFromLineBeginning(remove);
73
79
  }
74
80
  }
75
81
  function table(config) {
@@ -188,7 +188,7 @@ function AdditionalButtons(props) {
188
188
  "aria-label": "Horizontal rule",
189
189
  className: styles.icon
190
190
  })), /*#__PURE__*/ React.createElement(ToolbarButton, {
191
- isDisabled: props.indentationDisabled,
191
+ isDisabled: props.indentDisabled,
192
192
  testId: "markdown-action-button-indent",
193
193
  tooltip: "Increase indentation",
194
194
  tooltipPlace: tooltipPlace,
@@ -197,7 +197,7 @@ function AdditionalButtons(props) {
197
197
  "aria-label": "Increase indentation",
198
198
  className: styles.icon
199
199
  })), /*#__PURE__*/ React.createElement(ToolbarButton, {
200
- isDisabled: props.indentationDisabled,
200
+ isDisabled: props.dedentDisabled,
201
201
  testId: "markdown-action-button-dedent",
202
202
  tooltip: "Decrease indentation",
203
203
  tooltipPlace: tooltipPlace,
@@ -9,7 +9,8 @@ configure({
9
9
  const createProps = ()=>({
10
10
  canUploadAssets: true,
11
11
  disabled: false,
12
- indentationDisabled: false,
12
+ indentDisabled: false,
13
+ dedentDisabled: false,
13
14
  mode: 'default',
14
15
  actions: {
15
16
  headings: {
@@ -74,10 +75,10 @@ describe('MarkdownToolbar', ()=>{
74
75
  name: 'Undo'
75
76
  })).toBeInTheDocument();
76
77
  });
77
- it('disables indentation controls when indentation is unavailable', async ()=>{
78
+ it('disables indent button when indent is unavailable', async ()=>{
78
79
  const props = {
79
80
  ...createProps(),
80
- indentationDisabled: true
81
+ indentDisabled: true
81
82
  };
82
83
  render(/*#__PURE__*/ React.createElement(MarkdownToolbar, props));
83
84
  await userEvent.click(screen.getByRole('button', {
@@ -86,6 +87,22 @@ describe('MarkdownToolbar', ()=>{
86
87
  expect(screen.getByRole('button', {
87
88
  name: 'Increase indentation'
88
89
  })).toBeDisabled();
90
+ expect(screen.getByRole('button', {
91
+ name: 'Decrease indentation'
92
+ })).not.toBeDisabled();
93
+ });
94
+ it('disables dedent button when dedent is unavailable', async ()=>{
95
+ const props = {
96
+ ...createProps(),
97
+ dedentDisabled: true
98
+ };
99
+ render(/*#__PURE__*/ React.createElement(MarkdownToolbar, props));
100
+ await userEvent.click(screen.getByRole('button', {
101
+ name: 'More actions'
102
+ }));
103
+ expect(screen.getByRole('button', {
104
+ name: 'Increase indentation'
105
+ })).not.toBeDisabled();
89
106
  expect(screen.getByRole('button', {
90
107
  name: 'Decrease indentation'
91
108
  })).toBeDisabled();
@@ -10,7 +10,7 @@ import { MarkdownToolbar } from '../components/MarkdownToolbar';
10
10
  import { openCheatsheetModal } from '../dialogs/CheatsheetModalDialog';
11
11
  import { createMarkdownActions } from '../MarkdownActions';
12
12
  import { MarkdownDialogType } from '../types';
13
- import { isMarkdownListItem } from '../utils/isMarkdownListItem';
13
+ import { canIndent, canDedent } from '../utils/indentation';
14
14
  const MarkdownPreview = /*#__PURE__*/ React.lazy(()=>import('../components/MarkdownPreview'));
15
15
  const styles = {
16
16
  root: css({
@@ -76,7 +76,8 @@ export const ZenModeModalDialog = (props)=>{
76
76
  const [currentValue, setCurrentValue] = React.useState(props.initialValue ?? '');
77
77
  const [showPreview, setShowPreview] = React.useState(true);
78
78
  const [editor, setEditor] = React.useState(null);
79
- const [isCurrentLineAListItem, setIsCurrentLineAListItem] = React.useState(false);
79
+ const [canIndentLine, setCanIndentLine] = React.useState(false);
80
+ const [canDedentLine, setCanDedentLine] = React.useState(false);
80
81
  React.useEffect(()=>{
81
82
  props.sdk?.window?.updateHeight('100%');
82
83
  }, []);
@@ -112,7 +113,8 @@ export const ZenModeModalDialog = (props)=>{
112
113
  }, /*#__PURE__*/ React.createElement(MarkdownToolbar, {
113
114
  mode: "zen",
114
115
  disabled: false,
115
- indentationDisabled: !isCurrentLineAListItem,
116
+ indentDisabled: !canIndentLine,
117
+ dedentDisabled: !canDedentLine,
116
118
  canUploadAssets: false,
117
119
  actions: actions
118
120
  })), /*#__PURE__*/ React.createElement(Grid.Item, {
@@ -129,14 +131,17 @@ export const ZenModeModalDialog = (props)=>{
129
131
  editor.setReadOnly(false);
130
132
  setEditor(editor);
131
133
  editor.focus();
134
+ const syncIndentState = ()=>{
135
+ const line = editor.getCurrentLine();
136
+ setCanIndentLine(canIndent(line));
137
+ setCanDedentLine(canDedent(line));
138
+ };
132
139
  editor.events.onChange((value)=>{
133
140
  setCurrentValue(value);
134
141
  props.saveValueToSDK(value);
135
- setIsCurrentLineAListItem(isMarkdownListItem(editor.getCurrentLine()));
136
- });
137
- editor.events.onCursorActivity(()=>{
138
- setIsCurrentLineAListItem(isMarkdownListItem(editor.getCurrentLine()));
142
+ syncIndentState();
139
143
  });
144
+ editor.events.onCursorActivity(syncIndentState);
140
145
  }
141
146
  })), showPreview && /*#__PURE__*/ React.createElement(Grid.Item, {
142
147
  className: styles.previewSplit
@@ -0,0 +1,12 @@
1
+ import { isMarkdownListItem } from './isMarkdownListItem';
2
+ export const MAX_PARAGRAPH_LEADING_SPACES = 2;
3
+ export function getLeadingSpaces(line) {
4
+ return line.match(/^ */)?.[0].length ?? 0;
5
+ }
6
+ export function canIndent(line, indentUnit = 2) {
7
+ if (isMarkdownListItem(line)) return true;
8
+ return getLeadingSpaces(line) + indentUnit <= MAX_PARAGRAPH_LEADING_SPACES;
9
+ }
10
+ export function canDedent(line) {
11
+ return getLeadingSpaces(line) > 0;
12
+ }
@@ -3,7 +3,8 @@ import { MarkdownActions } from '../types';
3
3
  interface MarkdownToolbarProps {
4
4
  canUploadAssets: boolean;
5
5
  disabled: boolean;
6
- indentationDisabled: boolean;
6
+ indentDisabled: boolean;
7
+ dedentDisabled: boolean;
7
8
  actions: MarkdownActions;
8
9
  mode: 'default' | 'zen';
9
10
  }
@@ -0,0 +1,4 @@
1
+ export declare const MAX_PARAGRAPH_LEADING_SPACES = 2;
2
+ export declare function getLeadingSpaces(line: string): number;
3
+ export declare function canIndent(line: string, indentUnit?: number): boolean;
4
+ export declare function canDedent(line: string): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-markdown",
3
- "version": "2.3.2",
3
+ "version": "2.3.3-canary.2+f87d656c",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -65,5 +65,5 @@
65
65
  "publishConfig": {
66
66
  "registry": "https://npm.pkg.github.com/"
67
67
  },
68
- "gitHead": "433e58f1f2cced617ba7039319e7adfb27d668ed"
68
+ "gitHead": "f87d656cf309593ad19cd1e3b03571614411638c"
69
69
  }