Skip to content

Commit

Permalink
CB-4567 add disableCopyData settings (#2378)
Browse files Browse the repository at this point in the history
Co-authored-by: Daria Marutkina <[email protected]>
  • Loading branch information
devnaumov and dariamarutkina authored Feb 13, 2024
1 parent 325cbef commit 9828b1d
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface IReactCodeMirrorProps extends React.PropsWithChildren {
getValue?: () => string;
extensions?: Map<Compartment, Extension>;
readonly?: boolean;
disableCopy?: boolean;
autoFocus?: boolean;
onChange?: (value: string, update: ViewUpdate) => void;
onCursorChange?: (selection: SelectionRange, update: ViewUpdate) => void;
Expand Down
12 changes: 11 additions & 1 deletion webapp/packages/plugin-codemirror6/src/ReactCodemirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const ReactCodemirror = observer<IReactCodeMirrorProps, IEditorRef>(
incomingValue,
extensions = new Map<Compartment, Extension>(),
readonly,
disableCopy,
autoFocus,
onChange,
onCursorChange,
Expand All @@ -40,7 +41,16 @@ export const ReactCodemirror = observer<IReactCodeMirrorProps, IEditorRef>(
value = value ?? getValue?.();
const currentExtensions = useRef<Map<Compartment, Extension>>(new Map());
const readOnlyFacet = useMemo(() => EditorView.editable.of(!readonly), [readonly]);
extensions = useCodemirrorExtensions(extensions, readOnlyFacet);
const eventHandlers = useMemo(
() =>
EditorView.domEventHandlers({
copy() {
return disableCopy;
},
}),
[disableCopy],
);
extensions = useCodemirrorExtensions(extensions, [readOnlyFacet, eventHandlers]);
const [container, setContainer] = useState<HTMLDivElement | null>(null);
const [view, setView] = useState<EditorView | null>(null);
const [incomingView, setIncomingView] = useState<EditorView | null>(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
import { useCallback } from 'react';

import { useObjectRef } from '@cloudbeaver/core-blocks';
import { useService } from '@cloudbeaver/core-di';
import { EventContext, EventStopPropagationFlag } from '@cloudbeaver/core-events';
import { copyToClipboard } from '@cloudbeaver/core-utils';
import { IResultSetColumnKey, IResultSetElementKey, ResultSetDataKeysUtils, ResultSetSelectAction } from '@cloudbeaver/plugin-data-viewer';
import {
DataViewerService,
IResultSetColumnKey,
IResultSetElementKey,
ResultSetDataKeysUtils,
ResultSetSelectAction,
} from '@cloudbeaver/plugin-data-viewer';

import type { IDataGridSelectionContext } from './DataGridSelection/DataGridSelectionContext';
import type { ITableData } from './TableDataContext';
Expand Down Expand Up @@ -62,23 +69,26 @@ export function useGridSelectedCellsCopy(
resultSetSelectAction: ResultSetSelectAction,
selectionContext: IDataGridSelectionContext,
) {
const dataViewerService = useService(DataViewerService);
const props = useObjectRef({ tableData, selectionContext, resultSetSelectAction });

const onKeydownHandler = useCallback((event: React.KeyboardEvent) => {
if ((event.ctrlKey || event.metaKey) && event.nativeEvent.code === EVENT_KEY_CODE.C) {
EventContext.set(event, EventStopPropagationFlag);

const focusedElement = props.resultSetSelectAction.getFocusedElement();
let value: string | null = null;
if (dataViewerService.canCopyData) {
const focusedElement = props.resultSetSelectAction.getFocusedElement();
let value: string | null = null;

if (Array.from(props.selectionContext.selectedCells.keys()).length > 0) {
value = getSelectedCellsValue(props.tableData, props.selectionContext.selectedCells);
} else if (focusedElement) {
value = getCellCopyValue(tableData, focusedElement);
}
if (Array.from(props.selectionContext.selectedCells.keys()).length > 0) {
value = getSelectedCellsValue(props.tableData, props.selectionContext.selectedCells);
} else if (focusedElement) {
value = getCellCopyValue(tableData, focusedElement);
}

if (value !== null) {
copyToClipboard(value);
if (value !== null) {
copyToClipboard(value);
}
}
}
}, []);
Expand Down
4 changes: 4 additions & 0 deletions webapp/packages/plugin-data-viewer/src/DataViewerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import { DataViewerSettingsService } from './DataViewerSettingsService';

@injectable()
export class DataViewerService {
get canCopyData() {
return !this.dataViewerSettingsService.settings.getValue('disableCopyData');
}

constructor(private readonly dataViewerSettingsService: DataViewerSettingsService) {}

isDataEditable(connection: Connection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { DATA_EDITOR_SETTINGS_GROUP } from './DATA_EDITOR_SETTINGS_GROUP';

const defaultSettings = schema.object({
disableEdit: schema.coerce.boolean().default(false),
disableCopyData: schema.coerce.boolean().default(false),
fetchMin: schema.coerce.number().default(100),
fetchMax: schema.coerce.number().default(5000),
fetchDefault: schema.coerce.number().default(200),
Expand Down Expand Up @@ -75,6 +76,13 @@ export class DataViewerSettingsService extends Dependency {
description: 'settings_data_editor_disable_edit_description',
group: DATA_EDITOR_SETTINGS_GROUP,
},
{
key: 'disableCopyData',
type: ESettingsValueType.Checkbox,
name: 'settings_data_editor_disable_data_copy_name',
description: 'settings_data_editor_disable_data_copy_description',
group: DATA_EDITOR_SETTINGS_GROUP,
},
{
key: 'fetchMin',
type: ESettingsValueType.Input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { isResultSetContentValue } from '../../DatabaseDataModel/Actions/ResultS
import { ResultSetSelectAction } from '../../DatabaseDataModel/Actions/ResultSet/ResultSetSelectAction';
import { useResultSetActions } from '../../DatabaseDataModel/Actions/ResultSet/useResultSetActions';
import type { IDatabaseResultSet } from '../../DatabaseDataModel/IDatabaseResultSet';
import { DataViewerService } from '../../DataViewerService';
import type { IDataValuePanelProps } from '../../TableViewer/ValuePanel/DataValuePanelService';
import { QuotaPlaceholder } from '../QuotaPlaceholder';
import { VALUE_PANEL_TOOLS_STYLES } from '../ValuePanelTools/VALUE_PANEL_TOOLS_STYLES';
Expand Down Expand Up @@ -53,6 +54,7 @@ const DEFAULT_CONTENT_TYPE = 'text/plain';
export const TextValuePresentation: TabContainerPanelComponent<IDataValuePanelProps<any, IDatabaseResultSet>> = observer(
function TextValuePresentation({ model, resultIndex, dataFormat }) {
const translate = useTranslate();
const dataViewerService = useService(DataViewerService);
const notificationService = useService(NotificationService);
const textValuePresentationService = useService(TextValuePresentationService);
const style = useStyles(styles, UNDERLINE_TAB_STYLES, VALUE_PANEL_TOOLS_STYLES);
Expand Down Expand Up @@ -182,6 +184,7 @@ export const TextValuePresentation: TabContainerPanelComponent<IDataValuePanelPr
lineWrapping={lineWrapping}
readonly={isReadonly}
extensions={extensions}
disableCopy={!dataViewerService.canCopyData}
onChange={valueChangeHandler}
/>
</Group>
Expand Down
2 changes: 2 additions & 0 deletions webapp/packages/plugin-data-viewer/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export default [
['settings_data_editor', 'Data Editor'],
['settings_data_editor_disable_edit_name', 'Disable Edit'],
['settings_data_editor_disable_edit_description', 'Disable editing of data in Data Viewer'],
['settings_data_editor_disable_data_copy_name', 'Disable Copy'],
['settings_data_editor_disable_data_copy_description', 'Disable copying of data in Data Viewer'],
['settings_data_editor_fetch_min_name', 'Minimum fetch size'],
['settings_data_editor_fetch_min_description', 'Minimum number of rows to fetch'],
['settings_data_editor_fetch_max_name', 'Maximum fetch size'],
Expand Down
2 changes: 2 additions & 0 deletions webapp/packages/plugin-data-viewer/src/locales/it.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export default [
['settings_data_editor', 'Data Editor'],
['settings_data_editor_disable_edit_name', 'Disable Edit'],
['settings_data_editor_disable_edit_description', 'Disable editing of data in Data Viewer'],
['settings_data_editor_disable_data_copy_name', 'Disable Copy'],
['settings_data_editor_disable_data_copy_description', 'Disable copying of data in Data Viewer'],
['settings_data_editor_fetch_min_name', 'Minimum fetch size'],
['settings_data_editor_fetch_min_description', 'Minimum number of rows to fetch'],
['settings_data_editor_fetch_max_name', 'Maximum fetch size'],
Expand Down
2 changes: 2 additions & 0 deletions webapp/packages/plugin-data-viewer/src/locales/ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export default [
['settings_data_editor', 'Редактор данных'],
['settings_data_editor_disable_edit_name', 'Отключить редактирование'],
['settings_data_editor_disable_edit_description', 'Отключить редактирование данных'],
['settings_data_editor_disable_data_copy_name', 'Отключить копирование'],
['settings_data_editor_disable_data_copy_description', 'Отключить копирование данных'],
['settings_data_editor_fetch_min_name', 'Минимальный размер выборки'],
['settings_data_editor_fetch_min_description', 'Минимальное количество строк для выборки'],
['settings_data_editor_fetch_max_name', 'Максимальный размер выборки'],
Expand Down
2 changes: 2 additions & 0 deletions webapp/packages/plugin-data-viewer/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export default [
['settings_data_editor', 'Data Editor'],
['settings_data_editor_disable_edit_name', 'Disable Edit'],
['settings_data_editor_disable_edit_description', 'Disable editing of data in Data Viewer'],
['settings_data_editor_disable_data_copy_name', 'Disable Copy'],
['settings_data_editor_disable_data_copy_description', 'Disable copying of data in Data Viewer'],
['settings_data_editor_fetch_min_name', 'Minimum fetch size'],
['settings_data_editor_fetch_min_description', 'Minimum number of rows to fetch'],
['settings_data_editor_fetch_max_name', 'Maximum fetch size'],
Expand Down

0 comments on commit 9828b1d

Please sign in to comment.