@atlaskit/editor-plugin-alignment 0.1.0
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.
- package/.eslintrc.js +18 -0
- package/CHANGELOG.md +1 -0
- package/LICENSE.md +13 -0
- package/README.md +30 -0
- package/dist/cjs/commands/index.js +62 -0
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/plugin.js +69 -0
- package/dist/cjs/pm-plugins/keymap.js +14 -0
- package/dist/cjs/pm-plugins/main.js +45 -0
- package/dist/cjs/pm-plugins/types.js +5 -0
- package/dist/cjs/ui/Alignment/AlignmentButton/index.js +38 -0
- package/dist/cjs/ui/Alignment/index.js +57 -0
- package/dist/cjs/ui/Alignment/styles.js +11 -0
- package/dist/cjs/ui/ToolbarAlignment/icon-map.js +36 -0
- package/dist/cjs/ui/ToolbarAlignment/index.js +174 -0
- package/dist/cjs/ui/ToolbarAlignment/messages.js +29 -0
- package/dist/cjs/ui/ToolbarAlignment/styles.js +13 -0
- package/dist/cjs/utils/index.js +27 -0
- package/dist/es2019/commands/index.js +56 -0
- package/dist/es2019/index.js +1 -0
- package/dist/es2019/plugin.js +57 -0
- package/dist/es2019/pm-plugins/keymap.js +8 -0
- package/dist/es2019/pm-plugins/main.js +34 -0
- package/dist/es2019/pm-plugins/types.js +1 -0
- package/dist/es2019/ui/Alignment/AlignmentButton/index.js +32 -0
- package/dist/es2019/ui/Alignment/index.js +52 -0
- package/dist/es2019/ui/Alignment/styles.js +7 -0
- package/dist/es2019/ui/ToolbarAlignment/icon-map.js +30 -0
- package/dist/es2019/ui/ToolbarAlignment/index.js +145 -0
- package/dist/es2019/ui/ToolbarAlignment/messages.js +23 -0
- package/dist/es2019/ui/ToolbarAlignment/styles.js +14 -0
- package/dist/es2019/utils/index.js +15 -0
- package/dist/esm/commands/index.js +56 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/plugin.js +62 -0
- package/dist/esm/pm-plugins/keymap.js +8 -0
- package/dist/esm/pm-plugins/main.js +36 -0
- package/dist/esm/pm-plugins/types.js +1 -0
- package/dist/esm/ui/Alignment/AlignmentButton/index.js +31 -0
- package/dist/esm/ui/Alignment/index.js +49 -0
- package/dist/esm/ui/Alignment/styles.js +4 -0
- package/dist/esm/ui/ToolbarAlignment/icon-map.js +29 -0
- package/dist/esm/ui/ToolbarAlignment/index.js +168 -0
- package/dist/esm/ui/ToolbarAlignment/messages.js +23 -0
- package/dist/esm/ui/ToolbarAlignment/styles.js +6 -0
- package/dist/esm/utils/index.js +21 -0
- package/dist/types/commands/index.d.ts +4 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/plugin.d.ts +5 -0
- package/dist/types/pm-plugins/keymap.d.ts +2 -0
- package/dist/types/pm-plugins/main.d.ts +8 -0
- package/dist/types/pm-plugins/types.d.ts +5 -0
- package/dist/types/ui/Alignment/AlignmentButton/index.d.ts +13 -0
- package/dist/types/ui/Alignment/index.d.ts +12 -0
- package/dist/types/ui/Alignment/styles.d.ts +1 -0
- package/dist/types/ui/ToolbarAlignment/icon-map.d.ts +6 -0
- package/dist/types/ui/ToolbarAlignment/index.d.ts +33 -0
- package/dist/types/ui/ToolbarAlignment/messages.d.ts +22 -0
- package/dist/types/ui/ToolbarAlignment/styles.d.ts +3 -0
- package/dist/types/utils/index.d.ts +3 -0
- package/dist/types-ts4.5/commands/index.d.ts +4 -0
- package/dist/types-ts4.5/index.d.ts +2 -0
- package/dist/types-ts4.5/plugin.d.ts +5 -0
- package/dist/types-ts4.5/pm-plugins/keymap.d.ts +2 -0
- package/dist/types-ts4.5/pm-plugins/main.d.ts +8 -0
- package/dist/types-ts4.5/pm-plugins/types.d.ts +5 -0
- package/dist/types-ts4.5/ui/Alignment/AlignmentButton/index.d.ts +13 -0
- package/dist/types-ts4.5/ui/Alignment/index.d.ts +12 -0
- package/dist/types-ts4.5/ui/Alignment/styles.d.ts +1 -0
- package/dist/types-ts4.5/ui/ToolbarAlignment/icon-map.d.ts +6 -0
- package/dist/types-ts4.5/ui/ToolbarAlignment/index.d.ts +33 -0
- package/dist/types-ts4.5/ui/ToolbarAlignment/messages.d.ts +22 -0
- package/dist/types-ts4.5/ui/ToolbarAlignment/styles.d.ts +3 -0
- package/dist/types-ts4.5/utils/index.d.ts +3 -0
- package/package.json +96 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getActiveAlignment = void 0;
|
|
7
|
+
var _utils = require("@atlaskit/editor-prosemirror/utils");
|
|
8
|
+
var _cellSelection = require("@atlaskit/editor-tables/cell-selection");
|
|
9
|
+
var getActiveAlignment = exports.getActiveAlignment = function getActiveAlignment(state) {
|
|
10
|
+
if (state.selection instanceof _cellSelection.CellSelection) {
|
|
11
|
+
var marks = [];
|
|
12
|
+
state.selection.forEachCell(function (cell) {
|
|
13
|
+
var mark = cell.firstChild.marks.filter(function (mark) {
|
|
14
|
+
return mark.type === state.schema.marks.alignment;
|
|
15
|
+
})[0];
|
|
16
|
+
marks.push(mark ? mark.attrs.align : 'start');
|
|
17
|
+
});
|
|
18
|
+
return marks.every(function (mark) {
|
|
19
|
+
return mark === marks[0];
|
|
20
|
+
}) ? marks[0] : 'start';
|
|
21
|
+
}
|
|
22
|
+
var node = (0, _utils.findParentNodeOfType)([state.schema.nodes.paragraph, state.schema.nodes.heading])(state.selection);
|
|
23
|
+
var getMark = node && node.node.marks.filter(function (mark) {
|
|
24
|
+
return mark.type === state.schema.marks.alignment;
|
|
25
|
+
})[0];
|
|
26
|
+
return getMark && getMark.attrs.align || 'start';
|
|
27
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { changeImageAlignment, toggleBlockMark } from '@atlaskit/editor-common/commands';
|
|
2
|
+
import { Selection } from '@atlaskit/editor-prosemirror/state';
|
|
3
|
+
/**
|
|
4
|
+
* Iterates over the commands one after the other,
|
|
5
|
+
* passes the tr through and dispatches the cumulated transaction
|
|
6
|
+
*/
|
|
7
|
+
function cascadeCommands(cmds) {
|
|
8
|
+
return (state, dispatch) => {
|
|
9
|
+
let {
|
|
10
|
+
tr: baseTr
|
|
11
|
+
} = state;
|
|
12
|
+
let shouldDispatch = false;
|
|
13
|
+
const onDispatchAction = tr => {
|
|
14
|
+
const selectionJSON = tr.selection.toJSON();
|
|
15
|
+
baseTr.setSelection(Selection.fromJSON(baseTr.doc, selectionJSON));
|
|
16
|
+
tr.steps.forEach(st => {
|
|
17
|
+
baseTr.step(st);
|
|
18
|
+
});
|
|
19
|
+
shouldDispatch = true;
|
|
20
|
+
};
|
|
21
|
+
cmds.forEach(cmd => cmd(state, onDispatchAction));
|
|
22
|
+
if (dispatch && shouldDispatch) {
|
|
23
|
+
dispatch(baseTr);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export const isAlignable = align => (state, dispatch) => {
|
|
30
|
+
const {
|
|
31
|
+
nodes: {
|
|
32
|
+
paragraph,
|
|
33
|
+
heading
|
|
34
|
+
},
|
|
35
|
+
marks: {
|
|
36
|
+
alignment
|
|
37
|
+
}
|
|
38
|
+
} = state.schema;
|
|
39
|
+
return toggleBlockMark(alignment, () => !align ? undefined : align === 'start' ? false : {
|
|
40
|
+
align
|
|
41
|
+
}, [paragraph, heading])(state, dispatch);
|
|
42
|
+
};
|
|
43
|
+
export const changeAlignment = align => (state, dispatch) => {
|
|
44
|
+
const {
|
|
45
|
+
nodes: {
|
|
46
|
+
paragraph,
|
|
47
|
+
heading
|
|
48
|
+
},
|
|
49
|
+
marks: {
|
|
50
|
+
alignment
|
|
51
|
+
}
|
|
52
|
+
} = state.schema;
|
|
53
|
+
return cascadeCommands([changeImageAlignment(align), toggleBlockMark(alignment, () => !align ? undefined : align === 'start' ? false : {
|
|
54
|
+
align
|
|
55
|
+
}, [paragraph, heading])])(state, dispatch);
|
|
56
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { alignmentPlugin } from './plugin';
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { alignment } from '@atlaskit/adf-schema';
|
|
3
|
+
import { WithPluginState } from '@atlaskit/editor-common/with-plugin-state';
|
|
4
|
+
import { changeAlignment } from './commands';
|
|
5
|
+
import { keymapPlugin } from './pm-plugins/keymap';
|
|
6
|
+
import { createPlugin, pluginKey } from './pm-plugins/main';
|
|
7
|
+
import ToolbarAlignment from './ui/ToolbarAlignment';
|
|
8
|
+
export const defaultConfig = {
|
|
9
|
+
align: 'start'
|
|
10
|
+
};
|
|
11
|
+
export const alignmentPlugin = () => ({
|
|
12
|
+
name: 'alignment',
|
|
13
|
+
marks() {
|
|
14
|
+
return [{
|
|
15
|
+
name: 'alignment',
|
|
16
|
+
mark: alignment
|
|
17
|
+
}];
|
|
18
|
+
},
|
|
19
|
+
pmPlugins() {
|
|
20
|
+
return [{
|
|
21
|
+
name: 'alignmentPlugin',
|
|
22
|
+
plugin: ({
|
|
23
|
+
dispatch
|
|
24
|
+
}) => createPlugin(dispatch, defaultConfig)
|
|
25
|
+
}, {
|
|
26
|
+
name: 'annotationKeymap',
|
|
27
|
+
plugin: () => keymapPlugin()
|
|
28
|
+
}];
|
|
29
|
+
},
|
|
30
|
+
primaryToolbarComponent({
|
|
31
|
+
editorView,
|
|
32
|
+
popupsMountPoint,
|
|
33
|
+
popupsBoundariesElement,
|
|
34
|
+
popupsScrollableElement,
|
|
35
|
+
disabled,
|
|
36
|
+
isToolbarReducedSpacing
|
|
37
|
+
}) {
|
|
38
|
+
return /*#__PURE__*/React.createElement(WithPluginState, {
|
|
39
|
+
plugins: {
|
|
40
|
+
align: pluginKey
|
|
41
|
+
},
|
|
42
|
+
render: ({
|
|
43
|
+
align
|
|
44
|
+
}) => {
|
|
45
|
+
return /*#__PURE__*/React.createElement(ToolbarAlignment, {
|
|
46
|
+
pluginState: align,
|
|
47
|
+
isReducedSpacing: isToolbarReducedSpacing,
|
|
48
|
+
changeAlignment: align => changeAlignment(align)(editorView.state, editorView.dispatch),
|
|
49
|
+
disabled: disabled || !align.isEnabled,
|
|
50
|
+
popupsMountPoint: popupsMountPoint,
|
|
51
|
+
popupsBoundariesElement: popupsBoundariesElement,
|
|
52
|
+
popupsScrollableElement: popupsScrollableElement
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { alignLeft, bindKeymapWithCommand } from '@atlaskit/editor-common/keymaps';
|
|
2
|
+
import { keymap } from '@atlaskit/editor-prosemirror/keymap';
|
|
3
|
+
import { changeAlignment } from '../commands';
|
|
4
|
+
export function keymapPlugin() {
|
|
5
|
+
const list = {};
|
|
6
|
+
bindKeymapWithCommand(alignLeft.common, changeAlignment('start'), list);
|
|
7
|
+
return keymap(list);
|
|
8
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
|
|
2
|
+
import { PluginKey } from '@atlaskit/editor-prosemirror/state';
|
|
3
|
+
import { isAlignable } from '../commands';
|
|
4
|
+
import { getActiveAlignment } from '../utils';
|
|
5
|
+
export function createInitialPluginState(editorState, pluginConfig) {
|
|
6
|
+
return {
|
|
7
|
+
align: getActiveAlignment(editorState) || pluginConfig.align,
|
|
8
|
+
isEnabled: true
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export const pluginKey = new PluginKey('alignmentPlugin');
|
|
12
|
+
export function createPlugin(dispatch, pluginConfig) {
|
|
13
|
+
return new SafePlugin({
|
|
14
|
+
key: pluginKey,
|
|
15
|
+
state: {
|
|
16
|
+
init(_, editorState) {
|
|
17
|
+
return createInitialPluginState(editorState, pluginConfig);
|
|
18
|
+
},
|
|
19
|
+
apply(_tr, state, _prevState, nextState) {
|
|
20
|
+
const nextPluginState = getActiveAlignment(nextState);
|
|
21
|
+
const isEnabled = isAlignable(nextPluginState)(nextState);
|
|
22
|
+
const newState = {
|
|
23
|
+
...state,
|
|
24
|
+
align: nextPluginState,
|
|
25
|
+
isEnabled
|
|
26
|
+
};
|
|
27
|
+
if (nextPluginState !== state.align || isEnabled !== state.isEnabled) {
|
|
28
|
+
dispatch(pluginKey, newState);
|
|
29
|
+
}
|
|
30
|
+
return newState;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { getAriaKeyshortcuts, tooltip, ToolTipContent } from '@atlaskit/editor-common/keymaps';
|
|
3
|
+
import { ToolbarButton } from '@atlaskit/editor-common/ui-menu';
|
|
4
|
+
function AlignmentButton({
|
|
5
|
+
label,
|
|
6
|
+
isSelected,
|
|
7
|
+
content,
|
|
8
|
+
shortcut,
|
|
9
|
+
onClick,
|
|
10
|
+
value
|
|
11
|
+
}) {
|
|
12
|
+
const onClickCallback = e => {
|
|
13
|
+
// detect if the click event comes from keyboard where screenX and screenY are 0
|
|
14
|
+
const isMouseEvent = e instanceof MouseEvent;
|
|
15
|
+
e.preventDefault();
|
|
16
|
+
onClick(value, isMouseEvent);
|
|
17
|
+
};
|
|
18
|
+
return /*#__PURE__*/React.createElement(ToolbarButton, {
|
|
19
|
+
disabled: false,
|
|
20
|
+
selected: isSelected,
|
|
21
|
+
title: /*#__PURE__*/React.createElement(ToolTipContent, {
|
|
22
|
+
description: label,
|
|
23
|
+
keymap: shortcut
|
|
24
|
+
}),
|
|
25
|
+
"aria-label": shortcut ? tooltip(shortcut, label) : label,
|
|
26
|
+
"aria-pressed": isSelected,
|
|
27
|
+
"aria-keyshortcuts": getAriaKeyshortcuts(shortcut),
|
|
28
|
+
onClick: onClickCallback,
|
|
29
|
+
iconBefore: content
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export default AlignmentButton;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** @jsx jsx */
|
|
2
|
+
import { jsx } from '@emotion/react';
|
|
3
|
+
import { injectIntl } from 'react-intl-next';
|
|
4
|
+
import { alignLeft } from '@atlaskit/editor-common/keymaps';
|
|
5
|
+
import { alignmentMessages } from '@atlaskit/editor-common/messages';
|
|
6
|
+
import { IconMap } from '../ToolbarAlignment/icon-map';
|
|
7
|
+
import AlignmentButton from './AlignmentButton';
|
|
8
|
+
import { alignmentWrapper } from './styles';
|
|
9
|
+
const alignmentOptions = [{
|
|
10
|
+
title: alignmentMessages.alignLeft,
|
|
11
|
+
shortcut: alignLeft,
|
|
12
|
+
value: 'start'
|
|
13
|
+
}, {
|
|
14
|
+
title: alignmentMessages.alignCenter,
|
|
15
|
+
value: 'center'
|
|
16
|
+
}, {
|
|
17
|
+
title: alignmentMessages.alignRight,
|
|
18
|
+
value: 'end'
|
|
19
|
+
}];
|
|
20
|
+
function Alignment({
|
|
21
|
+
onClick,
|
|
22
|
+
selectedAlignment,
|
|
23
|
+
className,
|
|
24
|
+
intl
|
|
25
|
+
}) {
|
|
26
|
+
return jsx("div", {
|
|
27
|
+
"data-testid": "alignment-buttons"
|
|
28
|
+
// eslint-disable-next-line @atlaskit/design-system/consistent-css-prop-usage
|
|
29
|
+
,
|
|
30
|
+
css: alignmentWrapper,
|
|
31
|
+
className: className
|
|
32
|
+
}, alignmentOptions.map(alignment => {
|
|
33
|
+
const {
|
|
34
|
+
value,
|
|
35
|
+
title,
|
|
36
|
+
shortcut
|
|
37
|
+
} = alignment;
|
|
38
|
+
const message = intl.formatMessage(title);
|
|
39
|
+
return jsx(AlignmentButton, {
|
|
40
|
+
content: jsx(IconMap, {
|
|
41
|
+
alignment: value
|
|
42
|
+
}),
|
|
43
|
+
key: value,
|
|
44
|
+
value: value,
|
|
45
|
+
label: message,
|
|
46
|
+
shortcut: shortcut,
|
|
47
|
+
onClick: onClick,
|
|
48
|
+
isSelected: value === selectedAlignment
|
|
49
|
+
});
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
export default injectIntl(Alignment);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useIntl } from 'react-intl-next';
|
|
3
|
+
import EditorAlignCenterIcon from '@atlaskit/icon/glyph/editor/align-center';
|
|
4
|
+
import EditorAlignLeftIcon from '@atlaskit/icon/glyph/editor/align-left';
|
|
5
|
+
import EditorAlignRightIcon from '@atlaskit/icon/glyph/editor/align-right';
|
|
6
|
+
import { messages } from './messages';
|
|
7
|
+
const iconAndMessageMap = {
|
|
8
|
+
start: {
|
|
9
|
+
Component: EditorAlignLeftIcon,
|
|
10
|
+
label: messages.alignLeft
|
|
11
|
+
},
|
|
12
|
+
end: {
|
|
13
|
+
Component: EditorAlignRightIcon,
|
|
14
|
+
label: messages.alignRight
|
|
15
|
+
},
|
|
16
|
+
center: {
|
|
17
|
+
Component: EditorAlignCenterIcon,
|
|
18
|
+
label: messages.alignCenter
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
export const IconMap = props => {
|
|
22
|
+
const {
|
|
23
|
+
Component,
|
|
24
|
+
label
|
|
25
|
+
} = iconAndMessageMap[props.alignment];
|
|
26
|
+
const intl = useIntl();
|
|
27
|
+
return /*#__PURE__*/React.createElement(Component, {
|
|
28
|
+
label: intl.formatMessage(label)
|
|
29
|
+
});
|
|
30
|
+
};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
|
+
/** @jsx jsx */
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { jsx } from '@emotion/react';
|
|
5
|
+
import { injectIntl } from 'react-intl-next';
|
|
6
|
+
import { separatorStyles } from '@atlaskit/editor-common/styles';
|
|
7
|
+
import { ArrowKeyNavigationType, DropdownContainer as Dropdown, ToolbarButton } from '@atlaskit/editor-common/ui-menu';
|
|
8
|
+
import ExpandIcon from '@atlaskit/icon/glyph/chevron-down';
|
|
9
|
+
import Alignment from '../Alignment';
|
|
10
|
+
import { IconMap } from './icon-map';
|
|
11
|
+
import { messages } from './messages';
|
|
12
|
+
import { expandIconWrapper, triggerWrapper, wrapper } from './styles';
|
|
13
|
+
// eslint-disable-next-line @repo/internal/react/no-class-components
|
|
14
|
+
export class AlignmentToolbar extends React.Component {
|
|
15
|
+
constructor(...args) {
|
|
16
|
+
super(...args);
|
|
17
|
+
_defineProperty(this, "toolbarItemRef", /*#__PURE__*/React.createRef());
|
|
18
|
+
_defineProperty(this, "state", {
|
|
19
|
+
isOpen: false
|
|
20
|
+
});
|
|
21
|
+
_defineProperty(this, "changeAlignment", (align, togglePopup) => {
|
|
22
|
+
if (togglePopup) {
|
|
23
|
+
this.toggleOpen();
|
|
24
|
+
}
|
|
25
|
+
return this.props.changeAlignment(align);
|
|
26
|
+
});
|
|
27
|
+
_defineProperty(this, "toggleOpen", () => {
|
|
28
|
+
this.setState({
|
|
29
|
+
isOpen: !this.state.isOpen
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
_defineProperty(this, "toggleOpenByKeyboard", event => {
|
|
33
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
34
|
+
event.preventDefault();
|
|
35
|
+
this.setState({
|
|
36
|
+
isOpen: !this.state.isOpen
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
_defineProperty(this, "hide", attrs => {
|
|
41
|
+
if (this.state.isOpen) {
|
|
42
|
+
this.setState({
|
|
43
|
+
isOpen: false
|
|
44
|
+
});
|
|
45
|
+
if ((attrs === null || attrs === void 0 ? void 0 : attrs.event) instanceof KeyboardEvent && attrs.event.key === 'Escape') {
|
|
46
|
+
var _this$toolbarItemRef, _this$toolbarItemRef$;
|
|
47
|
+
(_this$toolbarItemRef = this.toolbarItemRef) === null || _this$toolbarItemRef === void 0 ? void 0 : (_this$toolbarItemRef$ = _this$toolbarItemRef.current) === null || _this$toolbarItemRef$ === void 0 ? void 0 : _this$toolbarItemRef$.focus();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
_defineProperty(this, "hideOnEscape", () => {
|
|
52
|
+
var _this$toolbarItemRef2, _this$toolbarItemRef3;
|
|
53
|
+
this.hide();
|
|
54
|
+
(_this$toolbarItemRef2 = this.toolbarItemRef) === null || _this$toolbarItemRef2 === void 0 ? void 0 : (_this$toolbarItemRef3 = _this$toolbarItemRef2.current) === null || _this$toolbarItemRef3 === void 0 ? void 0 : _this$toolbarItemRef3.focus();
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
render() {
|
|
58
|
+
const {
|
|
59
|
+
isOpen
|
|
60
|
+
} = this.state;
|
|
61
|
+
const {
|
|
62
|
+
popupsMountPoint,
|
|
63
|
+
popupsBoundariesElement,
|
|
64
|
+
popupsScrollableElement,
|
|
65
|
+
isReducedSpacing,
|
|
66
|
+
pluginState,
|
|
67
|
+
disabled,
|
|
68
|
+
intl
|
|
69
|
+
} = this.props;
|
|
70
|
+
const title = intl.formatMessage(messages.alignment);
|
|
71
|
+
return (
|
|
72
|
+
// eslint-disable-next-line @atlaskit/design-system/consistent-css-prop-usage
|
|
73
|
+
jsx("span", {
|
|
74
|
+
css: wrapper
|
|
75
|
+
}, jsx(Dropdown, {
|
|
76
|
+
mountTo: popupsMountPoint,
|
|
77
|
+
boundariesElement: popupsBoundariesElement,
|
|
78
|
+
scrollableElement: popupsScrollableElement,
|
|
79
|
+
isOpen: isOpen,
|
|
80
|
+
handleClickOutside: event => {
|
|
81
|
+
if (event instanceof MouseEvent) {
|
|
82
|
+
this.hide({
|
|
83
|
+
isOpen: false,
|
|
84
|
+
event
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
handleEscapeKeydown: this.hideOnEscape,
|
|
89
|
+
arrowKeyNavigationProviderOptions: {
|
|
90
|
+
type: ArrowKeyNavigationType.MENU
|
|
91
|
+
},
|
|
92
|
+
fitWidth: 112,
|
|
93
|
+
fitHeight: 80,
|
|
94
|
+
closeOnTab: true,
|
|
95
|
+
trigger: jsx(ToolbarButton, {
|
|
96
|
+
spacing: isReducedSpacing ? 'none' : 'default',
|
|
97
|
+
disabled: disabled,
|
|
98
|
+
selected: isOpen,
|
|
99
|
+
title: title,
|
|
100
|
+
className: "align-btn",
|
|
101
|
+
"aria-label": title,
|
|
102
|
+
"aria-expanded": isOpen,
|
|
103
|
+
"aria-haspopup": true,
|
|
104
|
+
onClick: this.toggleOpen,
|
|
105
|
+
onKeyDown: this.toggleOpenByKeyboard,
|
|
106
|
+
iconBefore:
|
|
107
|
+
// eslint-disable-next-line @atlaskit/design-system/consistent-css-prop-usage
|
|
108
|
+
jsx("div", {
|
|
109
|
+
css: triggerWrapper
|
|
110
|
+
}, jsx(IconMap, {
|
|
111
|
+
alignment: pluginState.align
|
|
112
|
+
}), jsx("span", {
|
|
113
|
+
css: expandIconWrapper
|
|
114
|
+
}, jsx(ExpandIcon, {
|
|
115
|
+
label: ""
|
|
116
|
+
}))),
|
|
117
|
+
ref: this.toolbarItemRef
|
|
118
|
+
})
|
|
119
|
+
}, jsx(Alignment, {
|
|
120
|
+
onClick: align => this.changeAlignment(align, false),
|
|
121
|
+
selectedAlignment: pluginState.align
|
|
122
|
+
})), jsx("span", {
|
|
123
|
+
css: separatorStyles
|
|
124
|
+
}))
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
componentDidUpdate(prevProps) {
|
|
128
|
+
if (this.state.isOpen) {
|
|
129
|
+
// by triggering the keyboard event with a setTimeout, we ensure that the tooltip
|
|
130
|
+
// associated with the alignment button doesn't render until the next render cycle
|
|
131
|
+
// where the popup will be correctly positioned and the relative position of the tooltip
|
|
132
|
+
// will not overlap with the button.
|
|
133
|
+
setTimeout(() => {
|
|
134
|
+
var _this$toolbarItemRef$2;
|
|
135
|
+
const keyboardEvent = new KeyboardEvent('keydown', {
|
|
136
|
+
bubbles: true,
|
|
137
|
+
key: 'ArrowDown'
|
|
138
|
+
});
|
|
139
|
+
(_this$toolbarItemRef$2 = this.toolbarItemRef.current) === null || _this$toolbarItemRef$2 === void 0 ? void 0 : _this$toolbarItemRef$2.dispatchEvent(keyboardEvent);
|
|
140
|
+
}, 0);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
_defineProperty(AlignmentToolbar, "displayName", 'AlignmentToolbar');
|
|
145
|
+
export default injectIntl(AlignmentToolbar);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { defineMessages } from 'react-intl-next';
|
|
2
|
+
export const messages = defineMessages({
|
|
3
|
+
alignment: {
|
|
4
|
+
id: 'fabric.editor.alignment',
|
|
5
|
+
defaultMessage: 'Text alignment',
|
|
6
|
+
description: 'Opens drop down menu of options to configure text alignment'
|
|
7
|
+
},
|
|
8
|
+
alignLeft: {
|
|
9
|
+
id: 'fabric.editor.alignLeft',
|
|
10
|
+
defaultMessage: 'Align left',
|
|
11
|
+
description: 'label stating that text is aligned left'
|
|
12
|
+
},
|
|
13
|
+
alignRight: {
|
|
14
|
+
id: 'fabric.editor.alignRight',
|
|
15
|
+
defaultMessage: 'Align Right',
|
|
16
|
+
description: 'label stating that text is aligned right'
|
|
17
|
+
},
|
|
18
|
+
alignCenter: {
|
|
19
|
+
id: 'fabric.editor.alignCenter',
|
|
20
|
+
defaultMessage: 'Align Center',
|
|
21
|
+
description: 'label stating that text is aligned center'
|
|
22
|
+
}
|
|
23
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { css } from '@emotion/react';
|
|
2
|
+
export const triggerWrapper = css`
|
|
3
|
+
display: flex;
|
|
4
|
+
`;
|
|
5
|
+
export const wrapper = css`
|
|
6
|
+
display: flex;
|
|
7
|
+
align-items: center;
|
|
8
|
+
div {
|
|
9
|
+
display: flex;
|
|
10
|
+
}
|
|
11
|
+
`;
|
|
12
|
+
export const expandIconWrapper = css`
|
|
13
|
+
margin-left: ${"var(--ds-space-negative-100, -8px)"};
|
|
14
|
+
`;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { findParentNodeOfType } from '@atlaskit/editor-prosemirror/utils';
|
|
2
|
+
import { CellSelection } from '@atlaskit/editor-tables/cell-selection';
|
|
3
|
+
export const getActiveAlignment = state => {
|
|
4
|
+
if (state.selection instanceof CellSelection) {
|
|
5
|
+
const marks = [];
|
|
6
|
+
state.selection.forEachCell(cell => {
|
|
7
|
+
const mark = cell.firstChild.marks.filter(mark => mark.type === state.schema.marks.alignment)[0];
|
|
8
|
+
marks.push(mark ? mark.attrs.align : 'start');
|
|
9
|
+
});
|
|
10
|
+
return marks.every(mark => mark === marks[0]) ? marks[0] : 'start';
|
|
11
|
+
}
|
|
12
|
+
const node = findParentNodeOfType([state.schema.nodes.paragraph, state.schema.nodes.heading])(state.selection);
|
|
13
|
+
const getMark = node && node.node.marks.filter(mark => mark.type === state.schema.marks.alignment)[0];
|
|
14
|
+
return getMark && getMark.attrs.align || 'start';
|
|
15
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { changeImageAlignment, toggleBlockMark } from '@atlaskit/editor-common/commands';
|
|
2
|
+
import { Selection } from '@atlaskit/editor-prosemirror/state';
|
|
3
|
+
/**
|
|
4
|
+
* Iterates over the commands one after the other,
|
|
5
|
+
* passes the tr through and dispatches the cumulated transaction
|
|
6
|
+
*/
|
|
7
|
+
function cascadeCommands(cmds) {
|
|
8
|
+
return function (state, dispatch) {
|
|
9
|
+
var baseTr = state.tr;
|
|
10
|
+
var shouldDispatch = false;
|
|
11
|
+
var onDispatchAction = function onDispatchAction(tr) {
|
|
12
|
+
var selectionJSON = tr.selection.toJSON();
|
|
13
|
+
baseTr.setSelection(Selection.fromJSON(baseTr.doc, selectionJSON));
|
|
14
|
+
tr.steps.forEach(function (st) {
|
|
15
|
+
baseTr.step(st);
|
|
16
|
+
});
|
|
17
|
+
shouldDispatch = true;
|
|
18
|
+
};
|
|
19
|
+
cmds.forEach(function (cmd) {
|
|
20
|
+
return cmd(state, onDispatchAction);
|
|
21
|
+
});
|
|
22
|
+
if (dispatch && shouldDispatch) {
|
|
23
|
+
dispatch(baseTr);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export var isAlignable = function isAlignable(align) {
|
|
30
|
+
return function (state, dispatch) {
|
|
31
|
+
var _state$schema = state.schema,
|
|
32
|
+
_state$schema$nodes = _state$schema.nodes,
|
|
33
|
+
paragraph = _state$schema$nodes.paragraph,
|
|
34
|
+
heading = _state$schema$nodes.heading,
|
|
35
|
+
alignment = _state$schema.marks.alignment;
|
|
36
|
+
return toggleBlockMark(alignment, function () {
|
|
37
|
+
return !align ? undefined : align === 'start' ? false : {
|
|
38
|
+
align: align
|
|
39
|
+
};
|
|
40
|
+
}, [paragraph, heading])(state, dispatch);
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
export var changeAlignment = function changeAlignment(align) {
|
|
44
|
+
return function (state, dispatch) {
|
|
45
|
+
var _state$schema2 = state.schema,
|
|
46
|
+
_state$schema2$nodes = _state$schema2.nodes,
|
|
47
|
+
paragraph = _state$schema2$nodes.paragraph,
|
|
48
|
+
heading = _state$schema2$nodes.heading,
|
|
49
|
+
alignment = _state$schema2.marks.alignment;
|
|
50
|
+
return cascadeCommands([changeImageAlignment(align), toggleBlockMark(alignment, function () {
|
|
51
|
+
return !align ? undefined : align === 'start' ? false : {
|
|
52
|
+
align: align
|
|
53
|
+
};
|
|
54
|
+
}, [paragraph, heading])])(state, dispatch);
|
|
55
|
+
};
|
|
56
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { alignmentPlugin } from './plugin';
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { alignment } from '@atlaskit/adf-schema';
|
|
3
|
+
import { WithPluginState } from '@atlaskit/editor-common/with-plugin-state';
|
|
4
|
+
import { changeAlignment as _changeAlignment } from './commands';
|
|
5
|
+
import { keymapPlugin } from './pm-plugins/keymap';
|
|
6
|
+
import { createPlugin, pluginKey } from './pm-plugins/main';
|
|
7
|
+
import ToolbarAlignment from './ui/ToolbarAlignment';
|
|
8
|
+
export var defaultConfig = {
|
|
9
|
+
align: 'start'
|
|
10
|
+
};
|
|
11
|
+
export var alignmentPlugin = function alignmentPlugin() {
|
|
12
|
+
return {
|
|
13
|
+
name: 'alignment',
|
|
14
|
+
marks: function marks() {
|
|
15
|
+
return [{
|
|
16
|
+
name: 'alignment',
|
|
17
|
+
mark: alignment
|
|
18
|
+
}];
|
|
19
|
+
},
|
|
20
|
+
pmPlugins: function pmPlugins() {
|
|
21
|
+
return [{
|
|
22
|
+
name: 'alignmentPlugin',
|
|
23
|
+
plugin: function plugin(_ref) {
|
|
24
|
+
var dispatch = _ref.dispatch;
|
|
25
|
+
return createPlugin(dispatch, defaultConfig);
|
|
26
|
+
}
|
|
27
|
+
}, {
|
|
28
|
+
name: 'annotationKeymap',
|
|
29
|
+
plugin: function plugin() {
|
|
30
|
+
return keymapPlugin();
|
|
31
|
+
}
|
|
32
|
+
}];
|
|
33
|
+
},
|
|
34
|
+
primaryToolbarComponent: function primaryToolbarComponent(_ref2) {
|
|
35
|
+
var editorView = _ref2.editorView,
|
|
36
|
+
popupsMountPoint = _ref2.popupsMountPoint,
|
|
37
|
+
popupsBoundariesElement = _ref2.popupsBoundariesElement,
|
|
38
|
+
popupsScrollableElement = _ref2.popupsScrollableElement,
|
|
39
|
+
disabled = _ref2.disabled,
|
|
40
|
+
isToolbarReducedSpacing = _ref2.isToolbarReducedSpacing;
|
|
41
|
+
return /*#__PURE__*/React.createElement(WithPluginState, {
|
|
42
|
+
plugins: {
|
|
43
|
+
align: pluginKey
|
|
44
|
+
},
|
|
45
|
+
render: function render(_ref3) {
|
|
46
|
+
var align = _ref3.align;
|
|
47
|
+
return /*#__PURE__*/React.createElement(ToolbarAlignment, {
|
|
48
|
+
pluginState: align,
|
|
49
|
+
isReducedSpacing: isToolbarReducedSpacing,
|
|
50
|
+
changeAlignment: function changeAlignment(align) {
|
|
51
|
+
return _changeAlignment(align)(editorView.state, editorView.dispatch);
|
|
52
|
+
},
|
|
53
|
+
disabled: disabled || !align.isEnabled,
|
|
54
|
+
popupsMountPoint: popupsMountPoint,
|
|
55
|
+
popupsBoundariesElement: popupsBoundariesElement,
|
|
56
|
+
popupsScrollableElement: popupsScrollableElement
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { alignLeft, bindKeymapWithCommand } from '@atlaskit/editor-common/keymaps';
|
|
2
|
+
import { keymap } from '@atlaskit/editor-prosemirror/keymap';
|
|
3
|
+
import { changeAlignment } from '../commands';
|
|
4
|
+
export function keymapPlugin() {
|
|
5
|
+
var list = {};
|
|
6
|
+
bindKeymapWithCommand(alignLeft.common, changeAlignment('start'), list);
|
|
7
|
+
return keymap(list);
|
|
8
|
+
}
|