fix: 卡片的 ui 组件点击执行命令保持光标在卡片上

This commit is contained in:
yanmao 2022-01-15 22:39:29 +08:00
parent a35ea1cd7b
commit b5f805654a
4 changed files with 41 additions and 16 deletions

View File

@ -1,5 +1,11 @@
import { TRIGGER_CARD_ID, UI_SELECTOR } from './constants';
import { isMarkPlugin } from './plugin'; import { isMarkPlugin } from './plugin';
import { ChangeInterface, EditorInterface, CommandInterface } from './types'; import {
ChangeInterface,
EditorInterface,
CommandInterface,
CardInterface,
} from './types';
import { isEngine } from './utils'; import { isEngine } from './utils';
/** /**
@ -71,8 +77,21 @@ class Command implements CommandInterface {
if ( if (
!range.commonAncestorNode.isRoot() && !range.commonAncestorNode.isRoot() &&
!range.commonAncestorNode.inEditor() !range.commonAncestorNode.inEditor()
) ) {
this.editor.focus(); const uiElement = range.commonAncestorNode.closest(UI_SELECTOR);
let component: CardInterface | undefined = undefined;
if (uiElement.length > 0) {
const cardId = uiElement.attributes(TRIGGER_CARD_ID);
if (cardId) {
const { card } = this.editor;
component = card.find(cardId);
if (component) {
card.select(component);
}
}
}
if (!component) this.editor.focus();
}
change.cacheRangeBeforeCommand(); change.cacheRangeBeforeCommand();
} }
return change; return change;

View File

@ -1,4 +1,10 @@
import { $, ConversionData, MarkPlugin, PluginOptions } from '@aomao/engine'; import {
$,
ConversionData,
MarkPlugin,
PluginOptions,
SchemaMark,
} from '@aomao/engine';
export interface BackcolorOptions extends PluginOptions { export interface BackcolorOptions extends PluginOptions {
hotkey?: { key: string; args: Array<string> }; hotkey?: { key: string; args: Array<string> };
@ -12,7 +18,6 @@ export default class<T extends BackcolorOptions> extends MarkPlugin<T> {
style = { style = {
'background-color': '@var0', 'background-color': '@var0',
background: '@var1',
}; };
variable = { variable = {
@ -20,9 +25,14 @@ export default class<T extends BackcolorOptions> extends MarkPlugin<T> {
required: true, required: true,
value: '@color', value: '@color',
}, },
'@var1': '@color',
}; };
schema(): SchemaMark | SchemaMark[] {
const schemas = super.schema() as SchemaMark;
schemas.attributes!.style['background'] = '@color';
return schemas;
}
conversion(): ConversionData { conversion(): ConversionData {
return [ return [
{ {

View File

@ -16,7 +16,7 @@ export type Options = {
onBlur?: () => void; onBlur?: () => void;
onChange?: ( onChange?: (
value: string, value: string,
color: { color?: {
background: string; background: string;
color: string; color: string;
}, },
@ -44,7 +44,7 @@ class StatusEditor {
change() { change() {
const { onChange } = this.options; const { onChange } = this.options;
if (onChange) onChange(this.#value!, this.#color!); if (onChange) onChange(this.#value!, this.#color);
} }
updateActive(color: { background: string; color: string }) { updateActive(color: { background: string; color: string }) {
@ -55,6 +55,8 @@ class StatusEditor {
); );
if (index > -1) { if (index > -1) {
svgElements?.eq(index)?.css('display', 'block'); svgElements?.eq(index)?.css('display', 'block');
} else {
this.#color = undefined;
} }
} }

View File

@ -84,14 +84,8 @@ class Status<T extends StatusValue = StatusValue> extends Card<T> {
this.#statusEditor = new StatusEditor({ this.#statusEditor = new StatusEditor({
colors: this.getColors(), colors: this.getColors(),
onChange: ( onChange: (text, color) => {
text: string, if (color) this.setColor(color);
color: {
background: string;
color: string;
},
) => {
this.setColor(color);
this.setValue({ this.setValue({
text, text,
} as T); } as T);