uni-app text、文本、selectable、自定义长按选择菜单、修改系统菜单键(双端)

uni-app text、文本、selectable、自定义长按选择菜单、修改系统菜单键(双端):https://ext.dcloud.net.cn/plugin?id=10586

<template>
    <view>
        <KJ-LongPressMenu id="gray" class="gray" ref="gray" :style="{height:textHeight}" :text="text"
            :selectable="selectable" :longPressMenu="longPressMenu" @onLongPressMenuItemClick="onLongPressMenuItemClick"
            @onHeight="onHeight">
        </KJ-LongPressMenu>
        <KJ-LongPressMenu id="gray2" class="gray2" ref="gray2" :style="{height:textHeight2}" :text="text2"
            :selectable="selectable" :longPressMenu="longPressMenu" @onLongPressMenuItemClick="onLongPressMenuItemClick"
            @onHeight="onHeight">
        </KJ-LongPressMenu>
        <button class="btn" type="primary" @click="selectable_m()">是否可选择</button>
        <button class="btn" type="primary" @click="text_m()">改变文本内容</button>
        <button class="btn" type="primary" @click="longPressMenu_m()">只展示自定义菜单item</button>
        <button class="btn" type="primary" @click="longPressMenu_m2()">只展示系统菜单item</button>
        <button class="btn" type="primary" @click="longPressMenu_m3()">展示自定义和系统菜单item</button>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                textHeight: "100px",
                textHeight2: "100px",
                selectable: true,
                text: "",
                text2: "",
                longPressMenu: null
            }
        },
        onLoad() {
            this.text = "我是可长按自定义选择菜单的view\n当内容大于高度时,可滚动"
            this.text2 = "我是可长按自定义选择菜单的view\n高度可自适应";
            this.longPressMenu_m();
        },
        methods: {
            onHeight(res) { //高度自适应,可以用这个方法,动态获取高度
                console.log("onHeight:" + JSON.stringify(res))
                if (res.target.id == "gray2") {
                    var height = res.detail.height + 1;
                    this.textHeight2 = height + "px"
                }
            },
            onLongPressMenuItemClick(res) { // 只会回调customs的item
                console.log("onLongPressMenuItemClick:" + JSON.stringify(res))
                uni.showModal({
                    content: "点击的item的name:" + res.detail.name + "     选中的内容:" + res.detail.selectedText
                })
            },
            selectable_m() {
                this.selectable = !this.selectable;
            },
            text_m() {
                this.text =
                    "uni-app 是一个使用 Vue.js 开发所有前端应用的框架,开发者编写一套代码,可发布到iOS、Android、Web(响应式)、以及各种小程序(微信/支付宝/百度/头条/飞书/QQ/快手/钉钉/淘宝)、快应用等多个平台。\n" +
                    "DCloud公司拥有900万开发者、数百万应用、12亿手机端月活用户、数千款uni-app插件、70+微信/qq群。阿里小程序工具官方内置uni-app(详见),腾讯课堂官方为uni-app录制培训课程(详见),开发者可以放心选择。\n" +
                    "uni-app在手,做啥都不愁。即使不跨端,uni-app也是更好的小程序开发框架(详见)、更好的App跨平台框架、更方便的H5开发框架。不管领导安排什么样的项目,你都可以快速交付,不需要转换开发思维、不需要更改开发习惯"
                this.text2 = this.text;
            },
            longPressMenu_m() {
                /**
                 * systems method 只支持:copy(复制) selectAll(全选) _share(分享)
                 * */
                this.longPressMenu = {
                    "customs": [{
                        "name": "自定义1",
                    }, {
                        "name": "自定义2",
                    }, {
                        "name": "自定义3",
                    }, {
                        "name": "自定义4",
                    }, {
                        "name": "自定义5",
                    }, {
                        "name": "自定义6",
                    }, {
                        "name": "自定义7",
                    }, {
                        "name": "自定义8",
                    }, {
                        "name": "自定义9",
                    }, {
                        "name": "自定义10",
                    }],
                    "systems": null
                }
            },
            longPressMenu_m2() {
                this.longPressMenu = {
                    "customs": null,
                    "systems": [{
                        "method": "copy"
                    }, {
                        "method": "selectAll"
                    }, {
                        "method": "_share"
                    }]
                }
            },
            longPressMenu_m3() {
                this.longPressMenu = null;
                this.longPressMenu = {
                    "customs": [{
                        "name": "自定义1",
                    }, {
                        "name": "自定义2",
                    }],
                    "systems": [{
                        "method": "copy"
                    }, {
                        "method": "selectAll"
                    }, {
                        "method": "_share"
                    }]
                }
            }
        }
    }
</script>
<style>
    .kongbai {
        width: 750rpx;
        height: 1000px;
    }

    .gray,
    .gray2 {
        width: 750rpx;
        /**
         * 关于文字风格,支持以下属性
         */
        background-color: aqua;
        font-size: 15px;
        color: red;
        text-align: center;
    }

    .gray2 {
        background-color: white;
    }
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较复杂的功能,需要结合Vue3的指令和事件处理来实现。我将分步骤给出实现的思路和代码示例: 1. 在对话气泡组件中添加按事件处理函数,同时使用Vue3的v-selectable指令使其支持按选中全部内容: ```html <template> <div class="bubble" v-long-press="onLongPress" v-selectable :class="{ selected: selected }" > {{ text }} </div> </template> <script> import { ref } from 'vue'; export default { props: { text: String, }, setup(props) { const selected = ref(false); const onLongPress = () => { selected.value = true; }; return { selected, onLongPress, }; }, }; </script> ``` 2. 使用CSS样式控制按选中内容的外观,并给选中的内容添加一个特殊的类名: ```css .bubble::selection { background-color: #b3d4fc; color: #000; } .bubble.selected::selection { background-color: #b3d4fc; color: #000; } ``` 3. 添加自定义菜单弹窗组件,并使用Vue3的Teleport组件将其挂载到body元素上: ```html <template> <teleport to="body"> <div v-show="visible" class="menu"> <div class="menu-item" @click="onCopy">复制</div> <div class="menu-item" @click="onDelete">删除</div> </div> </teleport> </template> <script> import { ref } from 'vue'; export default { props: { x: Number, y: Number, }, setup(props) { const visible = ref(false); const onCopy = () => { document.execCommand('copy'); visible.value = false; }; const onDelete = () => { // TODO: 删除选中内容 visible.value = false; }; return { visible, onCopy, onDelete, }; }, }; </script> <style scoped> .menu { position: absolute; left: 0; top: 0; z-index: 999; background-color: #fff; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2); padding: 10px; border-radius: 4px; } .menu-item { cursor: pointer; padding: 5px 10px; margin-bottom: 5px; font-size: 14px; } </style> ``` 4. 在按事件处理函数中记录选中内容的位置和大小,并展示自定义菜单弹窗: ```html <template> <div class="bubble" v-long-press="onLongPress" v-selectable :class="{ selected: selected }" > {{ text }} <menu-popup v-if="selected" :x="x" :y="y" /> </div> </template> <script> import { ref, onMounted, onUnmounted } from 'vue'; import MenuPopup from './MenuPopup.vue'; export default { props: { text: String, }, components: { MenuPopup, }, setup(props) { const selected = ref(false); const x = ref(0); const y = ref(0); const onLongPress = (e) => { e.preventDefault(); selected.value = true; const range = window.getSelection().getRangeAt(0); const rect = range.getBoundingClientRect(); x.value = rect.left + window.scrollX; y.value = rect.top + window.scrollY; onMounted(() => { document.addEventListener('click', onClick); }); onUnmounted(() => { document.removeEventListener('click', onClick); }); }; const onClick = () => { selected.value = false; }; return { selected, x, y, onLongPress, }; }, }; </script> ``` 现在,我们已经完成了使用Vue3实现对话气泡按选中全部内容并弹出自定义菜单弹窗的功能。需要注意的是,上述代码中的菜单弹窗只是一个简单的示例,实际使用时可能需要根据具体需求进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值