Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extract editor modified logic to extensions #138

Merged
merged 2 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/controller/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,19 @@ export class EditorController extends Controller implements IEditorController {
const newValue = editorInstance.getModel()?.getValue();
const { current } = this.editorService.getState();
const tab = current?.tab;
const originValue = tab?.data?.value;
if (!tab) return;
const notSave = newValue !== tab?.data?.value;
mumiao marked this conversation as resolved.
Show resolved Hide resolved
this.editorService.updateTab(
{
id: tab.id,
data: {
...tab.data,
modified: notSave,
value: newValue,
},
},
groupId
);
this.emit(EditorEvent.OnUpdateTab, newValue, groupId, originValue);
this.emit(
FolderTreeEvent.onUpdateFileContent,
current?.tab?.id as any,
Expand Down
1 change: 1 addition & 0 deletions src/model/workbench/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum EditorEvent {
OnMoveTab = 'editor.moveTab',
OpenTab = 'editor.openTab',
OnSelectTab = 'editor.selectTab',
OnUpdateTab = 'editor.updateTab',
OnSplitEditorRight = 'editor.splitEditorRight',
}
interface BuiltInEditorTabDataType {
Expand Down
18 changes: 18 additions & 0 deletions src/services/workbench/editorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as monaco from 'monaco-editor';

import { Component } from 'mo/react';
import {
EditorEvent,
EditorModel,
EditorGroupModel,
IEditor,
Expand All @@ -30,6 +31,13 @@ export interface IEditorService extends Component<IEditor> {
closeAll(groupId: number): void;
getGroupById(groupId: number): IEditorGroup | undefined;
cloneGroup(groupId?: number): IEditorGroup;
onUpdateTab(
callback: (
newValue: string,
groupId: number,
originValue?: string
) => void
);
/**
* Set active group and tab
* @param groupId Target group ID
Expand Down Expand Up @@ -280,4 +288,14 @@ export class EditorService
});
return cloneGroup;
}

public onUpdateTab(
callback: (
newValue: string,
groupId: number,
originValue?: string
) => void
) {
this.subscribe(EditorEvent.OnUpdateTab, callback);
}
}
34 changes: 34 additions & 0 deletions stories/extensions/test/testPane.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import * as monaco from 'monaco-editor';
import {
activityBarService,
colorThemeService,
Expand Down Expand Up @@ -92,6 +93,39 @@ export type GenericClassDecorator<T> = (target: T) => void;`,
editorService.open(tabData);
};

editorService.onUpdateTab(
(newValue: string, groupId: number, originValue?: string) => {
const { current } = editorService.getState();
const tab = current?.tab!;
const notSave = newValue !== originValue;
editorService.updateTab(
{
id: tab.id,
data: {
...tab.data,
modified: notSave,
},
},
groupId
);
current?.editorInstance.addCommand(
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S,
() => {
// ctrl + s
editorService.updateTab(
{
id: tab.id,
data: {
...tab.data,
modified: false,
},
},
groupId
);
}
);
}
);
let notify;
const addANotification = function () {
notify = notificationService.addNotification<string>({
Expand Down