@contentful/field-editor-markdown 2.3.1 → 2.3.2-canary.3

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,6 +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
32
  function _interop_require_default(obj) {
32
33
  return obj && obj.__esModule ? obj : {
33
34
  default: obj
@@ -88,6 +89,7 @@ function MarkdownEditor(props) {
88
89
  const [selectedTab, setSelectedTab] = _react.useState('editor');
89
90
  const [editor, setEditor] = _react.useState(null);
90
91
  const [canUploadAssets, setCanUploadAssets] = _react.useState(false);
92
+ const [isCurrentLineAListItem, setIsCurrentLineAListItem] = _react.useState(false);
91
93
  _react.useEffect(()=>{
92
94
  if (props.enableTab) {
93
95
  setSelectedTab(props.enableTab);
@@ -129,6 +131,7 @@ function MarkdownEditor(props) {
129
131
  editor
130
132
  ]);
131
133
  const isActionDisabled = editor === null || props.isDisabled || selectedTab !== 'editor';
134
+ const isIndentationDisabled = isActionDisabled || !isCurrentLineAListItem;
132
135
  const direction = props.sdk.locales.direction[props.sdk.field.locale] ?? 'ltr';
133
136
  const actions = _react.useMemo(()=>{
134
137
  return (0, _MarkdownActions.createMarkdownActions)({
@@ -155,6 +158,7 @@ function MarkdownEditor(props) {
155
158
  }), /*#__PURE__*/ _react.createElement(_MarkdownToolbar.MarkdownToolbar, {
156
159
  mode: "default",
157
160
  disabled: isActionDisabled,
161
+ indentationDisabled: isIndentationDisabled,
158
162
  canUploadAssets: canUploadAssets,
159
163
  actions: actions
160
164
  }), /*#__PURE__*/ _react.createElement(_MarkdownTextarea.MarkdownTextarea, {
@@ -171,6 +175,10 @@ function MarkdownEditor(props) {
171
175
  const trimmedValue = value.replace(/^\s+$/gm, '');
172
176
  props.saveValueToSDK(trimmedValue);
173
177
  setCurrentValue(value);
178
+ setIsCurrentLineAListItem((0, _isMarkdownListItem.isMarkdownListItem)(editor.getCurrentLine()));
179
+ });
180
+ editor.events.onCursorActivity(()=>{
181
+ setIsCurrentLineAListItem((0, _isMarkdownListItem.isMarkdownListItem)(editor.getCurrentLine()));
174
182
  });
175
183
  }
176
184
  }), selectedTab === 'preview' && /*#__PURE__*/ _react.createElement(_react.Suspense, {
@@ -77,11 +77,15 @@ function createMarkdownEditor(host, options) {
77
77
  },
78
78
  onPaste: function(fn) {
79
79
  editor.attachEvent('paste', fn, 0);
80
+ },
81
+ onCursorActivity: function(fn) {
82
+ editor.attachEvent('cursorActivity', fn, 0);
80
83
  }
81
84
  },
82
85
  insert: editor.insertAtCursor,
83
86
  focus: editor.focus,
84
87
  getContent: editor.getValue,
88
+ getCurrentLine: editor.getCurrentLine,
85
89
  destroy: editor.destroy,
86
90
  setContent: editor.setValue,
87
91
  getSelectedText: editor.getSelectedText,
@@ -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.disabled,
258
+ isDisabled: props.indentationDisabled,
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.disabled,
267
+ isDisabled: props.indentationDisabled,
268
268
  testId: "markdown-action-button-dedent",
269
269
  tooltip: "Decrease indentation",
270
270
  tooltipPlace: tooltipPlace,
@@ -59,6 +59,7 @@ function _interop_require_wildcard(obj, nodeInterop) {
59
59
  const createProps = ()=>({
60
60
  canUploadAssets: true,
61
61
  disabled: false,
62
+ indentationDisabled: false,
62
63
  mode: 'default',
63
64
  actions: {
64
65
  headings: {
@@ -123,6 +124,22 @@ const createProps = ()=>({
123
124
  name: 'Undo'
124
125
  })).toBeInTheDocument();
125
126
  });
127
+ (0, _vitest.it)('disables indentation controls when indentation is unavailable', async ()=>{
128
+ const props = {
129
+ ...createProps(),
130
+ indentationDisabled: true
131
+ };
132
+ (0, _react1.render)(/*#__PURE__*/ _react.createElement(_MarkdownToolbar.MarkdownToolbar, props));
133
+ await _userevent.default.click(_react1.screen.getByRole('button', {
134
+ name: 'More actions'
135
+ }));
136
+ (0, _vitest.expect)(_react1.screen.getByRole('button', {
137
+ name: 'Increase indentation'
138
+ })).toBeDisabled();
139
+ (0, _vitest.expect)(_react1.screen.getByRole('button', {
140
+ name: 'Decrease indentation'
141
+ })).toBeDisabled();
142
+ });
126
143
  (0, _vitest.it)('triggers a toolbar action once per click', async ()=>{
127
144
  const props = createProps();
128
145
  (0, _react1.render)(/*#__PURE__*/ _react.createElement(_MarkdownToolbar.MarkdownToolbar, props));
@@ -28,6 +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
32
  function _interop_require_default(obj) {
32
33
  return obj && obj.__esModule ? obj : {
33
34
  default: obj
@@ -139,6 +140,7 @@ const ZenModeModalDialog = (props)=>{
139
140
  const [currentValue, setCurrentValue] = _react.useState(props.initialValue ?? '');
140
141
  const [showPreview, setShowPreview] = _react.useState(true);
141
142
  const [editor, setEditor] = _react.useState(null);
143
+ const [isCurrentLineAListItem, setIsCurrentLineAListItem] = _react.useState(false);
142
144
  _react.useEffect(()=>{
143
145
  props.sdk?.window?.updateHeight('100%');
144
146
  }, []);
@@ -174,6 +176,7 @@ const ZenModeModalDialog = (props)=>{
174
176
  }, /*#__PURE__*/ _react.createElement(_MarkdownToolbar.MarkdownToolbar, {
175
177
  mode: "zen",
176
178
  disabled: false,
179
+ indentationDisabled: !isCurrentLineAListItem,
177
180
  canUploadAssets: false,
178
181
  actions: actions
179
182
  })), /*#__PURE__*/ _react.createElement(_f36components.Grid.Item, {
@@ -193,6 +196,10 @@ const ZenModeModalDialog = (props)=>{
193
196
  editor.events.onChange((value)=>{
194
197
  setCurrentValue(value);
195
198
  props.saveValueToSDK(value);
199
+ setIsCurrentLineAListItem((0, _isMarkdownListItem.isMarkdownListItem)(editor.getCurrentLine()));
200
+ });
201
+ editor.events.onCursorActivity(()=>{
202
+ setIsCurrentLineAListItem((0, _isMarkdownListItem.isMarkdownListItem)(editor.getCurrentLine()));
196
203
  });
197
204
  }
198
205
  })), showPreview && /*#__PURE__*/ _react.createElement(_f36components.Grid.Item, {
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "isMarkdownListItem", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return isMarkdownListItem;
9
+ }
10
+ });
11
+ function isMarkdownListItem(line) {
12
+ return /^\s*(?:[-*+] |\d+[.)] )/.test(line);
13
+ }
@@ -10,6 +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
14
  const MarkdownPreview = /*#__PURE__*/ React.lazy(()=>import('./components/MarkdownPreview'));
14
15
  const styles = {
15
16
  container: css({
@@ -24,6 +25,7 @@ export function MarkdownEditor(props) {
24
25
  const [selectedTab, setSelectedTab] = React.useState('editor');
25
26
  const [editor, setEditor] = React.useState(null);
26
27
  const [canUploadAssets, setCanUploadAssets] = React.useState(false);
28
+ const [isCurrentLineAListItem, setIsCurrentLineAListItem] = React.useState(false);
27
29
  React.useEffect(()=>{
28
30
  if (props.enableTab) {
29
31
  setSelectedTab(props.enableTab);
@@ -65,6 +67,7 @@ export function MarkdownEditor(props) {
65
67
  editor
66
68
  ]);
67
69
  const isActionDisabled = editor === null || props.isDisabled || selectedTab !== 'editor';
70
+ const isIndentationDisabled = isActionDisabled || !isCurrentLineAListItem;
68
71
  const direction = props.sdk.locales.direction[props.sdk.field.locale] ?? 'ltr';
69
72
  const actions = React.useMemo(()=>{
70
73
  return createMarkdownActions({
@@ -91,6 +94,7 @@ export function MarkdownEditor(props) {
91
94
  }), /*#__PURE__*/ React.createElement(MarkdownToolbar, {
92
95
  mode: "default",
93
96
  disabled: isActionDisabled,
97
+ indentationDisabled: isIndentationDisabled,
94
98
  canUploadAssets: canUploadAssets,
95
99
  actions: actions
96
100
  }), /*#__PURE__*/ React.createElement(MarkdownTextarea, {
@@ -107,6 +111,10 @@ export function MarkdownEditor(props) {
107
111
  const trimmedValue = value.replace(/^\s+$/gm, '');
108
112
  props.saveValueToSDK(trimmedValue);
109
113
  setCurrentValue(value);
114
+ setIsCurrentLineAListItem(isMarkdownListItem(editor.getCurrentLine()));
115
+ });
116
+ editor.events.onCursorActivity(()=>{
117
+ setIsCurrentLineAListItem(isMarkdownListItem(editor.getCurrentLine()));
110
118
  });
111
119
  }
112
120
  }), selectedTab === 'preview' && /*#__PURE__*/ React.createElement(React.Suspense, {
@@ -26,11 +26,15 @@ export function createMarkdownEditor(host, options) {
26
26
  },
27
27
  onPaste: function(fn) {
28
28
  editor.attachEvent('paste', fn, 0);
29
+ },
30
+ onCursorActivity: function(fn) {
31
+ editor.attachEvent('cursorActivity', fn, 0);
29
32
  }
30
33
  },
31
34
  insert: editor.insertAtCursor,
32
35
  focus: editor.focus,
33
36
  getContent: editor.getValue,
37
+ getCurrentLine: editor.getCurrentLine,
34
38
  destroy: editor.destroy,
35
39
  setContent: editor.setValue,
36
40
  getSelectedText: editor.getSelectedText,
@@ -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.disabled,
191
+ isDisabled: props.indentationDisabled,
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.disabled,
200
+ isDisabled: props.indentationDisabled,
201
201
  testId: "markdown-action-button-dedent",
202
202
  tooltip: "Decrease indentation",
203
203
  tooltipPlace: tooltipPlace,
@@ -9,6 +9,7 @@ configure({
9
9
  const createProps = ()=>({
10
10
  canUploadAssets: true,
11
11
  disabled: false,
12
+ indentationDisabled: false,
12
13
  mode: 'default',
13
14
  actions: {
14
15
  headings: {
@@ -73,6 +74,22 @@ describe('MarkdownToolbar', ()=>{
73
74
  name: 'Undo'
74
75
  })).toBeInTheDocument();
75
76
  });
77
+ it('disables indentation controls when indentation is unavailable', async ()=>{
78
+ const props = {
79
+ ...createProps(),
80
+ indentationDisabled: true
81
+ };
82
+ render(/*#__PURE__*/ React.createElement(MarkdownToolbar, props));
83
+ await userEvent.click(screen.getByRole('button', {
84
+ name: 'More actions'
85
+ }));
86
+ expect(screen.getByRole('button', {
87
+ name: 'Increase indentation'
88
+ })).toBeDisabled();
89
+ expect(screen.getByRole('button', {
90
+ name: 'Decrease indentation'
91
+ })).toBeDisabled();
92
+ });
76
93
  it('triggers a toolbar action once per click', async ()=>{
77
94
  const props = createProps();
78
95
  render(/*#__PURE__*/ React.createElement(MarkdownToolbar, props));
@@ -10,6 +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
14
  const MarkdownPreview = /*#__PURE__*/ React.lazy(()=>import('../components/MarkdownPreview'));
14
15
  const styles = {
15
16
  root: css({
@@ -75,6 +76,7 @@ export const ZenModeModalDialog = (props)=>{
75
76
  const [currentValue, setCurrentValue] = React.useState(props.initialValue ?? '');
76
77
  const [showPreview, setShowPreview] = React.useState(true);
77
78
  const [editor, setEditor] = React.useState(null);
79
+ const [isCurrentLineAListItem, setIsCurrentLineAListItem] = React.useState(false);
78
80
  React.useEffect(()=>{
79
81
  props.sdk?.window?.updateHeight('100%');
80
82
  }, []);
@@ -110,6 +112,7 @@ export const ZenModeModalDialog = (props)=>{
110
112
  }, /*#__PURE__*/ React.createElement(MarkdownToolbar, {
111
113
  mode: "zen",
112
114
  disabled: false,
115
+ indentationDisabled: !isCurrentLineAListItem,
113
116
  canUploadAssets: false,
114
117
  actions: actions
115
118
  })), /*#__PURE__*/ React.createElement(Grid.Item, {
@@ -129,6 +132,10 @@ export const ZenModeModalDialog = (props)=>{
129
132
  editor.events.onChange((value)=>{
130
133
  setCurrentValue(value);
131
134
  props.saveValueToSDK(value);
135
+ setIsCurrentLineAListItem(isMarkdownListItem(editor.getCurrentLine()));
136
+ });
137
+ editor.events.onCursorActivity(()=>{
138
+ setIsCurrentLineAListItem(isMarkdownListItem(editor.getCurrentLine()));
132
139
  });
133
140
  }
134
141
  })), showPreview && /*#__PURE__*/ React.createElement(Grid.Item, {
@@ -0,0 +1,3 @@
1
+ export function isMarkdownListItem(line) {
2
+ return /^\s*(?:[-*+] |\d+[.)] )/.test(line);
3
+ }
@@ -36,10 +36,12 @@ export declare function createMarkdownEditor(host: HTMLElement, options: {
36
36
  onScroll: (fn: Function) => void;
37
37
  onChange: (fn: Function) => void;
38
38
  onPaste: (fn: Function) => void;
39
+ onCursorActivity: (fn: Function) => void;
39
40
  };
40
41
  insert: (text: string) => void;
41
42
  focus: () => void;
42
43
  getContent: () => string;
44
+ getCurrentLine: () => string;
43
45
  destroy: () => void;
44
46
  setContent: (value?: string | undefined) => void;
45
47
  getSelectedText: () => string;
@@ -3,6 +3,7 @@ import { MarkdownActions } from '../types';
3
3
  interface MarkdownToolbarProps {
4
4
  canUploadAssets: boolean;
5
5
  disabled: boolean;
6
+ indentationDisabled: boolean;
6
7
  actions: MarkdownActions;
7
8
  mode: 'default' | 'zen';
8
9
  }
@@ -0,0 +1 @@
1
+ export declare function isMarkdownListItem(line: string): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-markdown",
3
- "version": "2.3.1",
3
+ "version": "2.3.2-canary.3+82f89e24",
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": "78ed76ca4a3207924294971766a40bb46e196203"
68
+ "gitHead": "82f89e240070739f8b6ddc51278e2331df02fded"
69
69
  }