npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pk-markdown

v1.3.8

Published

pk项目的markdown编辑器,基于tui.editor,所见即所得(wysiwyg),增加了emoji组件和全屏组件

Downloads

49

Readme

pk-markdown

https://img.shields.io/badge/tui.editor-1.4.10-green https://img.shields.io/badge/vue-2.6.10-brightgreen https://img.shields.io/badge/vue--cli-3.9.2-orange https://img.shields.io/badge/jquery-3.4.1-yellowgreen https://img.shields.io/badge/v--viewer-1.4.2-yellowgreen https://img.shields.io/badge/axios-0.19.2-yellowgreen

pk生态服务平台 markdown组件,基于tui.editor,所见即所得(wysiwyg)

1.文档地址与demo

tui.editor

线上demo或dist目录下为demo文件

2.使用

npm i pk-markdown

<template>
  <div id="app">
    <pk-editor :upload-url="'/user-api/uploadFile/image'"/>
  </div>
</template>

<script>
  import PkMarkdown from "../package/pk-markdown"

  export default {
    name: 'App',
    components: {
      PkMarkdown
    }
  }
</script>

3.自定义属性

{
    value: { // 默认值
      type: String,
      default: ''
    },
    id: { // dom ID值,当一个页面有多个markdown时用id值进行区分
      type: String,
      required: false,
      default() {
        return `markdown-editor-${Math.floor(Math.random() * 100)}`
      }
    },
    options: { // editor 选项值,可参考官方文档
      type: Object,
      default() {
        return {}
      }
    },
    mode: { // 模式,有wysiwyg(所见即所得)和 markdown
      type: String,
      default: 'wysiwyg'
    },
    height: { // 容器高度
      type: String,
      required: false,
      default: '300px'
    },
    language: { // 语言
      type: String,
      required: false,
      default: 'zh_CN' // https://github.com/nhnent/tui.editor/tree/master/src/js/langs
    },
    viewer: { // 是否展示页
      type: Boolean,
      required: false,
      default: false
    },
    divideImg: { // 是否需要将图片与文字分隔展示(需要有一个.img-list的dom容器装图片,且默认最多九张
      type: Boolean,
      required: false,
      default: false
    },
    placeholder: { // placeholder
      type: String,
      default: ''
    },
    uploadUrl: { // 上传图片的地址,没有则默认转Base64
      type: String,
      default: ''
    },
    inputMode: { // 是否显示为输入框模式
      type: Boolean,
      default: false
    },
    imageMaxWidth: { // 图片展示时最大宽度
      type: Number,
      default: 800
    },
    imageMaxHeight: { // 图片展示时最大高度
      type: Number,
      default: 500
    }
  }

4.自定义功能

  1. 图片添加预览功能 在上传图片(指定了上传地址后)和viewer模式下调用parseImg可以对图片添加预览功能
parseImg () {
        setTimeout(() => {
          $('.tui-editor-contents').find('img:not(.viewer-image)').each((i, v) => {
          const style = this.getImageScale(v)
          const markedVue = new Vue({
            components: {
              Viewer
            },
            data() {
              return {
                image: v.src,
                style
              }
            },
            template: `
                <viewer style="display: inline-block" :options="{toolbar: false, title: false, navbar: false}" :images="[image]">
                  <img :style="style" alt="${v.alt}" :src="image" class="viewer-image"/>
                </viewer>`
          }).$mount()
          $(v).replaceWith(markedVue.$el)
        })
        }, 500)
      },

在这里插入图片描述

  1. 文字与图片分离展示功能 根据需求传入参数divideImg可以将此图片抽出加入至临近的.img-list元素中进行展示
divider () {
        setTimeout(() => {
          $(`#${this.id}`).find('img:not(.viewer-image)').each((i, v) => {
          const markedVue = new Vue({
            components: {
              Viewer
            },
            data() {
              return {
                image: v.src
              }
            },
            template: `
                  <viewer style="display: inline-block" :options="{toolbar: false, title: false, navbar: false}"
                          :images="[image]"><img :src="image"
                                                 class="viewer-image">
                  </viewer>`
          }).$mount()
          $(v).remove()
          const $targetDom = $(`#${this.id}`).next('.img-list')
          $targetDom.children().length < 9 ? $targetDom.append(markedVue.$el) : ''
        })
        }, 500)
      }

在这里插入图片描述

  1. 添加emoji表情
/**
       * 生成emoji按钮
       */
      initEmojiItem () {
        const emoji = `<button class="emoji"></button>`
        // 添加emoji
        this.toolbar.addItem({
          type: 'button',
          options: {
            name: 'emoji',
            $el: $(emoji),
            event: 'emojiButtonClicked',
            tooltip: 'emoji表情'
          }
        })
        const $emojiRoot = $('<ul></ul>')
        Object.values(emojiJson).map(v => {
          const emojiText = `&#x${v[0].substring(2)};`
          const $emoji = $(`<li class="emoji-icon">${emojiText}</li>`)
          $emoji.on('click', (e) => {
            this.editor.insertText(e.target.innerHTML)
          })
          $emojiRoot.append($emoji)
        })
        // 绑定点击emoji按钮事件
        const emojiButtonIndex = this.toolbar.indexOfItem('emoji')
        const $button = this.toolbar.getItem(emojiButtonIndex).$el
        this.editor.eventManager.addEventType('emojiButtonClicked')
        this.editor.eventManager.listen('emojiButtonClicked', () => {
          if (popup.isShow()) {
            popup.hide()
            return
          }

          const _$button$get = $button.get(0)
          const offsetTop = _$button$get.offsetTop
          const offsetLeft = _$button$get.offsetLeft

          popup.$el.css({
            top: offsetTop + $button.outerHeight(),
            right: _$button$get.parentElement.offsetWidth - offsetLeft - _$button$get.offsetWidth
          })

          popup.show()
        })
        // 生成emoji弹框
        const popup = this.editor.getUI().createPopup({
          header: false,
          title: false,
          content: $emojiRoot,
          className: 'emoji-list',
          $target: this.editor.getUI().getToolbar().$el,
          css: {
            'width': '300px',
            'height': '260px',
            'position': 'absolute'
          }
        })
        // 聚焦时弹框消失
        this.editor.eventManager.listen('focus', function () {
          popup.hide()
        })
      },

在这里插入图片描述

  1. 添加全屏功能
/**
       * 生成全屏非全屏按钮
       */
      initFullScreenItem () {
        const $root = this.editor.getUI().$el
        this.editor.eventManager.addEventType('toggleFullScreen')
        this.editor.eventManager.listen('toggleFullScreen', function () {
          const $fullscreen = $($root).find('.fullscreen')
          if ($fullscreen.hasClass('exit-fullscreen')) {
            $fullscreen.removeClass('exit-fullscreen')
          } else {
            $fullscreen.addClass('exit-fullscreen')
          }
          toggle.toggleFullScreen($root[0])
        })
        this.toolbar.addItem({
          type: 'button',
          options: {
            name: 'fullScreen',
            tooltip: '全屏/非全屏',
            event: 'toggleFullScreen',
            $el: $('<button class="fullscreen"></button>')
          }
        })
      },

在这里插入图片描述

  1. 限制图片展示时的比例大小
getImageScale(image) {
      const ratioOfImage = image.naturalWidth / image.naturalHeight
      if (ratioOfImage > 1) { // 宽大些,宽图
        if (image.naturalWidth > this.imageMaxWidth) { // 大于图片限制的最大宽度则进行等比缩小
          const height = image.naturalHeight / (image.naturalWidth / this.imageMaxWidth)
          return { width: `${this.imageMaxWidth}px`, height: `${height}px` }
        } else {
          return {}
        }
      } else { // 长大些,长图
        if (image.naturalHeight > this.imageMaxHeight) { // 大于图片限制的最大高度则进行等比缩小
          const width = image.naturalWidth / (image.naturalHeight / this.imageMaxHeight)
          return { height: `${this.imageMaxHeight}px`, width: `${width}px` }
        } else {
          return {}
        }
      }
    }