feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { type GrabNode } from '@coze-common/text-grab';
|
||||
import { WriteableAppLifeCycleService } from '@coze-common/chat-area';
|
||||
|
||||
import {
|
||||
PublicEventNames,
|
||||
type GrabPluginBizContext,
|
||||
} from '../../types/plugin-biz-context';
|
||||
|
||||
export class GrabAppLifeCycleService extends WriteableAppLifeCycleService<GrabPluginBizContext> {
|
||||
onBeforeDestroy(): void {
|
||||
const { unsubscribe, eventCenter, publicEventCenter, scene } =
|
||||
this.pluginInstance.pluginBizContext;
|
||||
|
||||
// Store 历史逻辑有一些问题,导致调用了 多次 destroy 但未初始化的情况,就不走下面的强制清理流程,而是走组件生命周期销毁
|
||||
if (scene === 'store') {
|
||||
return;
|
||||
}
|
||||
|
||||
unsubscribe();
|
||||
eventCenter.all.clear();
|
||||
publicEventCenter.all.clear();
|
||||
}
|
||||
onBeforeInitial(): void {
|
||||
const {
|
||||
publicEventCenter,
|
||||
grabPluginId: currentGrabPluginId,
|
||||
storeSet,
|
||||
} = this.pluginInstance.pluginBizContext;
|
||||
|
||||
const { useQuoteStore } = storeSet;
|
||||
|
||||
const { updateQuoteContent, updateQuoteVisible } = useQuoteStore.getState();
|
||||
|
||||
publicEventCenter.on(
|
||||
PublicEventNames.UpdateQuote,
|
||||
({
|
||||
grabPluginId,
|
||||
quote,
|
||||
}: {
|
||||
grabPluginId: string;
|
||||
quote: GrabNode[] | null;
|
||||
}) => {
|
||||
if (currentGrabPluginId !== grabPluginId) {
|
||||
return;
|
||||
}
|
||||
|
||||
updateQuoteContent(quote);
|
||||
updateQuoteVisible(true);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
type OnImageElementContext,
|
||||
WriteableCommandLifeCycleService,
|
||||
type OnLinkElementContext,
|
||||
} from '@coze-common/chat-area';
|
||||
|
||||
import {
|
||||
EventNames,
|
||||
type GrabPluginBizContext,
|
||||
} from '../../types/plugin-biz-context';
|
||||
|
||||
export class GrabCommandLifeCycleService extends WriteableCommandLifeCycleService<GrabPluginBizContext> {
|
||||
onViewScroll(): void {
|
||||
const { emit } = this.pluginInstance.pluginBizContext.eventCenter;
|
||||
emit(EventNames.OnViewScroll);
|
||||
}
|
||||
onCardLinkElementMouseEnter(ctx: OnLinkElementContext) {
|
||||
const { emit } = this.pluginInstance.pluginBizContext.eventCenter;
|
||||
emit(EventNames.OnLinkElementMouseEnter, {
|
||||
...ctx,
|
||||
type: 'image',
|
||||
});
|
||||
}
|
||||
onCardLinkElementMouseLeave(ctx: OnLinkElementContext) {
|
||||
const { emit } = this.pluginInstance.pluginBizContext.eventCenter;
|
||||
emit(EventNames.OnLinkElementMouseLeave, {
|
||||
...ctx,
|
||||
type: 'image',
|
||||
});
|
||||
}
|
||||
onMdBoxImageElementMouseEnter(ctx: OnImageElementContext): void {
|
||||
const { emit } = this.pluginInstance.pluginBizContext.eventCenter;
|
||||
emit(EventNames.OnLinkElementMouseEnter, {
|
||||
...ctx,
|
||||
type: 'image',
|
||||
});
|
||||
}
|
||||
onMdBoxImageElementMouseLeave(ctx: OnImageElementContext): void {
|
||||
const { emit } = this.pluginInstance.pluginBizContext.eventCenter;
|
||||
emit(EventNames.OnLinkElementMouseLeave, {
|
||||
...ctx,
|
||||
type: 'image',
|
||||
});
|
||||
}
|
||||
onMdBoxLinkElementMouseEnter(ctx: OnLinkElementContext): void {
|
||||
const { emit } = this.pluginInstance.pluginBizContext.eventCenter;
|
||||
emit(EventNames.OnLinkElementMouseEnter, {
|
||||
...ctx,
|
||||
type: 'link',
|
||||
});
|
||||
}
|
||||
onMdBoxLinkElementMouseLeave(ctx: OnLinkElementContext): void {
|
||||
const { emit } = this.pluginInstance.pluginBizContext.eventCenter;
|
||||
emit(EventNames.OnLinkElementMouseLeave, {
|
||||
...ctx,
|
||||
type: 'link',
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { getOriginContentText } from '@coze-common/text-grab';
|
||||
import {
|
||||
ContentType,
|
||||
type OnBeforeAppendSenderMessageIntoStore,
|
||||
WriteableMessageLifeCycleService,
|
||||
type OnAfterAppendSenderMessageIntoStore,
|
||||
} from '@coze-common/chat-area';
|
||||
|
||||
import {
|
||||
EventNames,
|
||||
type GrabPluginBizContext,
|
||||
} from '../../types/plugin-biz-context';
|
||||
|
||||
export class GrabMessageLifeCycleService extends WriteableMessageLifeCycleService<GrabPluginBizContext> {
|
||||
onBeforeAppendSenderMessageIntoStore(
|
||||
ctx: OnBeforeAppendSenderMessageIntoStore,
|
||||
) {
|
||||
const { quoteContent, updateQuoteContentMapByImmer } =
|
||||
this.pluginInstance.pluginBizContext.storeSet.useQuoteStore.getState();
|
||||
|
||||
if (!quoteContent || ctx.from === 'shortcut') {
|
||||
return ctx;
|
||||
}
|
||||
|
||||
const originMessage = ctx.message;
|
||||
|
||||
const newContent = {
|
||||
item_list: [
|
||||
{
|
||||
type: 'text',
|
||||
text: originMessage.content,
|
||||
},
|
||||
],
|
||||
refer_items: [] as unknown[],
|
||||
};
|
||||
|
||||
newContent.refer_items.push({
|
||||
type: ContentType.Text,
|
||||
text: getOriginContentText(quoteContent),
|
||||
});
|
||||
|
||||
const newMessage = {
|
||||
...originMessage,
|
||||
content_type: ContentType.Mix,
|
||||
content: JSON.stringify(newContent),
|
||||
content_obj: newContent,
|
||||
};
|
||||
|
||||
updateQuoteContentMapByImmer(quoteContentMap => {
|
||||
const localMessageId = newMessage.extra_info.local_message_id;
|
||||
if (!quoteContentMap[localMessageId]) {
|
||||
quoteContentMap[localMessageId] = quoteContent;
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
...ctx,
|
||||
message: newMessage,
|
||||
};
|
||||
}
|
||||
|
||||
onAfterAppendSenderMessageIntoStore(
|
||||
ctx: OnAfterAppendSenderMessageIntoStore,
|
||||
) {
|
||||
if (ctx.from === 'shortcut') {
|
||||
return;
|
||||
}
|
||||
|
||||
const { storeSet } = this.pluginInstance.pluginBizContext;
|
||||
const { updateQuoteVisible, updateQuoteContent } =
|
||||
storeSet.useQuoteStore.getState();
|
||||
updateQuoteVisible(false);
|
||||
updateQuoteContent(null);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
onAfterProcessReceiveMessage() {
|
||||
const { emit } = this.pluginInstance.pluginBizContext.eventCenter;
|
||||
emit(EventNames.OnMessageUpdate);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user