Skip to content

Commit

Permalink
build: 更新 postrelease 规则 (#79) (#80)
Browse files Browse the repository at this point in the history
<!--
首先,感谢你的贡献!😄

在维护者审核通过后会合并。
请确保填写以下 pull request 的信息,谢谢!~
-->

### 🤔 这个变动的性质是?

- [ ] 新特性提交
- [x] 日常 bug 修复
- [ ] 性能优化
- [ ] 功能增强
- [ ] 重构
- [ ] 代码风格优化
- [ ] 测试用例
- [ ] 其他改动(是关于什么的改动?)

### 🔗 相关 Issue

<!--
1. 描述相关需求的来源,如相关的 issue 讨论链接。
2. 例如 close #xxxx、 fix #xxxx
-->
无法通过右键菜单创建剪藏

### 💡 需求背景和解决方案

无法通过右键菜单创建剪藏
<!--
1. 要解决的具体问题。
2. 列出最终的 API 实现和用法。
3. 涉及UI/交互变动需要有截图或 GIF。
-->

### 📝 更新日志

<!--
从用户角度描述具体变化,以及可能的 breaking change 和其他风险。
-->
  • Loading branch information
ilimei authored Aug 3, 2023
1 parent e71e46f commit b530afc
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 64 deletions.
8 changes: 6 additions & 2 deletions src/components/lake-editor/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { InjectEditorPlugin } from './editor-plugin';
export interface EditorProps {
value: string;
onChange: (value: string) => void;
onLoad?: () => void;
children?: React.ReactElement;
}

Expand Down Expand Up @@ -82,11 +83,12 @@ const templateHtml = `
`;

export default forwardRef<IEditorRef, EditorProps>((props, ref) => {
const { value, onChange } = props;
const { value, onChange, onLoad } = props;
const [ _loading, setLoading ] = useState(true);

Check warning on line 87 in src/components/lake-editor/editor.tsx

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

'_loading' is assigned a value but never used
const [ editor, setEditor ] = useState<any>(null);

Check warning on line 88 in src/components/lake-editor/editor.tsx

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

Unexpected any. Specify a different type
const contextRef = useRef({
onChange: props.onChange,
onLoad: props.onLoad,
});
const rootNodeRef = useRef<{ div: HTMLDivElement | null }>({
div: null,
Expand Down Expand Up @@ -147,12 +149,14 @@ export default forwardRef<IEditorRef, EditorProps>((props, ref) => {
useEffect(() => {
if (!editor) return;
editor.setDocument('text/html', value);
contextRef.current?.onLoad?.();
}, [ editor, value ]);

// 更新回调
useEffect(() => {
contextRef.current.onChange = onChange;
}, [ onChange ]);
contextRef.current.onLoad = onLoad;
}, [ onChange, onLoad ]);

// 导出ref
useImperativeHandle(ref, () => ({
Expand Down
52 changes: 52 additions & 0 deletions src/core/action-listener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { GLOBAL_EVENTS } from '@/events';
import Chrome from './chrome';

type MessageSender = chrome.runtime.MessageSender;

type SendResponse = (response: boolean) => void;

export interface RequestMessage {
action: keyof typeof GLOBAL_EVENTS;
HTMLs?: string[];
}

export const ActionListener: {
currentType: string | null;
HTMLs: string[];
listener: ((request: RequestMessage) => void)[];
addListener: (callback: (request: RequestMessage) => void) => () => void;
getSelectHTMLs: () => string[];
} = {
currentType: null,
listener: [],
HTMLs: [],
getSelectHTMLs() {
const HTMLs = this.HTMLs;
this.HTMLs = [];
return HTMLs;
},
addListener(callback: (request: RequestMessage) => void) {
this.listener.push(callback);
return () => {
this.listener = this.listener.filter(item => item !== callback);
};
},
};

const onReceiveMessage = async (
request: RequestMessage,
_sender: MessageSender,
sendResponse: SendResponse,
) => {
ActionListener.listener.forEach(listener => listener(request));
if (request.action === GLOBAL_EVENTS.GET_SELECTED_TEXT) {
ActionListener.currentType = 'selection';
ActionListener.HTMLs = request.HTMLs || [];
}
sendResponse(true);
};

Chrome.runtime.onMessage.addListener(onReceiveMessage);
window.addEventListener('beforeunload', () => {
Chrome.runtime.onMessage.removeListener(onReceiveMessage);
});
12 changes: 6 additions & 6 deletions src/pages/inject/content-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ class App {
.appendTo(`.${this.maskCls}`)
.on('click', async () => {
const selectedElems = this.areaSelection.getSelectedElems();
const htmls = Array.from(selectedElems).map(elem =>
const HTMLs = Array.from(selectedElems).map(elem =>
$(elem).prop('outerHTML'),
);

Chrome.runtime.sendMessage(
{
action: GLOBAL_EVENTS.GET_SELECTED_HTML,
htmls,
HTMLs,
},
() => {
iframe.addClass('show');
Expand Down Expand Up @@ -142,14 +142,14 @@ class App {

private confirmSelection() {
const selectedElems = this.areaSelection.getSelectedElems();
const htmls = Array.from(selectedElems).map(elem =>
const HTMLs = Array.from(selectedElems).map(elem =>
$(elem).prop('outerHTML'),
);

Chrome.runtime.sendMessage(
{
action: GLOBAL_EVENTS.GET_SELECTED_HTML,
htmls,
HTMLs,
},
() => {
const { sandboxURL } = this;
Expand Down Expand Up @@ -251,7 +251,7 @@ class App {
*/
setTimeout(() => {
this.areaSelection.overlay.focus();
}, 300)
}, 300);
document.addEventListener('keydown', this.handleKeyDown);
}

Expand All @@ -268,7 +268,7 @@ class App {
const { selectionText } = data;
Chrome.runtime.sendMessage({
action: GLOBAL_EVENTS.GET_SELECTED_TEXT,
htmls: [ selectionText ],
HTMLs: [ selectionText ],
});
});

Expand Down
18 changes: 8 additions & 10 deletions src/pages/sandbox/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,7 @@ import styles from './App.module.less';
import { EditorValueContext } from './EditorValueContext';
import { Other } from './Other';
import SaveTo from './SaveTo';

type MessageSender = chrome.runtime.MessageSender;

type SendResponse = (response: any) => void;

interface RequestMessage {
action: keyof typeof GLOBAL_EVENTS;
htmls?: string[];
}
import { ActionListener } from '@/core/action-listener';

initI18N();

Expand Down Expand Up @@ -134,7 +126,7 @@ type TabName = 'save-to' | 'other';

const App = () => {
const [ editorValue, setEditorValue ] = useState([]);
const [ currentType, setCurrentType ] = useState(null);
const [ currentType, setCurrentType ] = useState(ActionListener.currentType);
const {
state: { account, forceUpgradeInfo, appReady },
onClose,
Expand All @@ -155,6 +147,12 @@ const App = () => {
setTab(e.target.value as unknown as TabName);
};

useEffect(() => {
if (currentType === null && ActionListener.currentType) {
setCurrentType(ActionListener.currentType);
}
}, [ currentType ]);

const isLogined = account?.id;

if (!appReady) return null;
Expand Down
81 changes: 35 additions & 46 deletions src/pages/sandbox/SaveTo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useContext, useRef } from 'react';
import React, { useState, useEffect, useContext, useRef, useCallback } from 'react';
import { ConfigProvider, Button, Select, message, Menu } from 'antd';
import classnames from 'classnames';
import { get as safeGet } from 'lodash';
Expand All @@ -16,20 +16,16 @@ import ClipperSvg from '@/assets/svg/clipper.svg';
import BookLogoSvg from '@/assets/svg/book-logo.svg';
import NoteLogoSvg from '@/assets/svg/note-logo.svg';
import styles from './SaveTo.module.less';

type MessageSender = chrome.runtime.MessageSender;

type SendResponse = (response: any) => void;

interface RequestMessage {
action: keyof typeof GLOBAL_EVENTS;
htmls?: string[];
}
import { ActionListener } from '@/core/action-listener';

const getBookmarkHtml = (tab: chrome.tabs.Tab) => {
return `<h2>${tab.title}</h2><p><a href="${tab.url}">${tab.title}</a></p>`;
};

const getCitation = (tab: chrome.tabs.Tab) => {
return `<p>来自: <a href="${tab.url}">${tab.url}</a></p>`;
};

const getCurrentTab = (): Promise<chrome.tabs.Tab> =>
new Promise(resolve => {
Chrome.tabs.getCurrent(tab => {
Expand Down Expand Up @@ -110,45 +106,36 @@ const useViewModel = props => {
});
}, []);

const onReceiveMessage = async (
request: RequestMessage,
_sender: MessageSender,
sendResponse: SendResponse,
) => {
switch (request.action) {
case GLOBAL_EVENTS.GET_SELECTED_HTML: {
const { htmls } = request;
const onLoad = useCallback(() => {
if (editorRef.current) {
const HTMLs = ActionListener.getSelectHTMLs();
(async () => {
const noteId = await getNoteId();
const processedHTMLs = await processHTMLs(htmls, noteId);
if (editorRef.current.isEmpty()) {
const initHtml = getBookmarkHtml(await getCurrentTab());
editorRef.current?.appendContent(initHtml);
}
editorRef.current?.appendContent(processedHTMLs.join(''), true);
sendResponse(true);
return;
}
case GLOBAL_EVENTS.GET_SELECTED_TEXT: {
const { htmls } = request;
if (editorRef.current.isEmpty()) {
const initHtml = getBookmarkHtml(await getCurrentTab());
editorRef.current?.appendContent(initHtml);
}
editorRef.current?.appendContent(htmls.join(''), true);
setCurrentType('selection');
sendResponse(true);
return;
}
default:
sendResponse(true);
const processedHTMLs = await processHTMLs(HTMLs, noteId);
editorRef.current?.appendContent(processedHTMLs.join(''));
editorRef.current?.appendContent(getCitation(await getCurrentTab()), true);
})();
}
};
}, []);

useEffect(() => {
Chrome.runtime.onMessage.addListener(onReceiveMessage);
return () => {
Chrome.runtime.onMessage.removeListener(onReceiveMessage);
};
return ActionListener.addListener(async request => {
switch (request.action) {
case GLOBAL_EVENTS.GET_SELECTED_HTML: {
const { HTMLs } = request;
const noteId = await getNoteId();
const processedHTMLs = await processHTMLs(HTMLs, noteId);
if (editorRef.current.isEmpty()) {
const initHtml = getBookmarkHtml(await getCurrentTab());
editorRef.current?.appendContent(initHtml);
}
editorRef.current?.appendContent(processedHTMLs.join(''), true);
return;
}
default:
break;
}
});
}, []);

useEffect(() => {
Expand Down Expand Up @@ -258,6 +245,7 @@ const useViewModel = props => {
onContinue,
onSelectType,
onSelectBookId,
onLoad,
};
};

Expand All @@ -269,6 +257,7 @@ const SaveTo = React.forwardRef<IEditorRef, any>((props, ref) => {
onSave,
onContinue,
onSelectType,
onLoad,
} = useViewModel(props);
const handleTypeSelect = (info: MenuInfo) => {
onSelectType(info.key);
Expand Down Expand Up @@ -325,7 +314,7 @@ const SaveTo = React.forwardRef<IEditorRef, any>((props, ref) => {
</Button>
{currentType && (
<div className={styles['lake-editor']}>
<LakeEditor ref={editorRef} value="" onChange={html => {
<LakeEditor ref={editorRef} value="" onLoad={onLoad} onChange={html => {
// console.info(html);
}}>
<Button onClick={onContinue}>
Expand Down

0 comments on commit b530afc

Please sign in to comment.