Skip to content

Commit

Permalink
選択したinputテキストをコンテキストメニューヘッダーに表示する
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkfx committed Aug 2, 2024
1 parent ba201cc commit 9bdb2e5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/components/Dialog/DictionaryManageDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
ref="surfaceContextMenu"
:header="surfaceContextMenuHeader"
:menudata="surfaceContextMenudata"
@beforeShow="readyForSurfaceContextMenu"
/>
</QInput>
</div>
Expand All @@ -152,6 +153,7 @@
ref="yomiContextMenu"
:header="yomiContextMenuHeader"
:menudata="yomiContextMenudata"
@beforeShow="readyForYomiContextMenu"
/>
</QInput>
</div>
Expand Down Expand Up @@ -684,12 +686,14 @@ const {
contextMenu: surfaceContextMenu,
contextMenuHeader: surfaceContextMenuHeader,
contextMenudata: surfaceContextMenudata,
readyForContextMenu: readyForSurfaceContextMenu,
} = useRightClickContextMenu(surfaceInput, surface, ref("surface"));
const {
contextMenu: yomiContextMenu,
contextMenuHeader: yomiContextMenuHeader,
contextMenudata: yomiContextMenudata,
readyForContextMenu: readyForYomiContextMenu,
} = useRightClickContextMenu(yomiInput, yomi, ref("yomi"));
</script>

Expand Down
36 changes: 36 additions & 0 deletions src/composables/useRightClickContextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,42 @@ export function useRightClickContextMenu(
const inputSelection = new SelectionHelperForQInput(qInputRef);

const contextMenu = ref<InstanceType<typeof ContextMenu>>();

const contextMenuHeader = ref<string | undefined>("");
const readyForContextMenu = () => {
const MAX_HEADER_LENGTH = 15;
const SHORTED_HEADER_FRAGMENT_LENGTH = 5;

const getMenuItemButton = (label: string) => {
const item = contextMenudata.value.find((item) => item.label === label);
if (item?.type !== "button")
throw new Error("コンテキストメニューアイテムの取得に失敗しました。");
return item;
};

const text = inputSelection.getAsString();
if (text.length === 0) {
getMenuItemButton("切り取り").disabled = true;
getMenuItemButton("コピー").disabled = true;
} else {
getMenuItemButton("切り取り").disabled = false;
getMenuItemButton("コピー").disabled = false;
if (text.length > MAX_HEADER_LENGTH) {
contextMenuHeader.value =
text.length <= MAX_HEADER_LENGTH
? text
: `${text.substring(
0,
SHORTED_HEADER_FRAGMENT_LENGTH,
)} ... ${text.substring(
text.length - SHORTED_HEADER_FRAGMENT_LENGTH,
)}`;
} else {
contextMenuHeader.value = text;
}
}
};

const contextMenudata = ref<
[
MenuItemButton,
Expand Down Expand Up @@ -117,5 +152,6 @@ export function useRightClickContextMenu(
contextMenu,
contextMenuHeader,
contextMenudata,
readyForContextMenu,
};
}

0 comments on commit 9bdb2e5

Please sign in to comment.