1.0.5 • Published 1 year ago

doc-note v1.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

doc-note是基于prismjs开发的语法高亮vue2组件, 支持编辑和提交.

vue3版doc-note -> 请看 um-note

Doc-note is a syntax highlighting vue2 component developed based on prismjs, which supports editing and submission

doc-note for vue3 -> see um-note

中文文档 | English Api

完整demo -> Demo & Sound Code

npm.io

-- 基本用法 --

- 下载依赖

npm i doc-note -S

-   注册组件

// main.js
import { DocNote, DocNoteConfig } from 'doc-note'

// DocNoteConfig是doc-note组件的配置方法, 相当于初始化方法, 必须在Vue.use(DocNote)之前执行.
DocNoteConfig()

Vue.use(DocNote)

* DocNoteConfig 配置

-   .vue文件中使用

<template>
  <doc-note :codes="code"/>
</template>
export default {
  data() {
    return {
      code: `const helloWord = 'Hello, doc-note!'`
    }
  }
}

-- props --

参数说明类型默认值
width组件的宽度. *必须带单位.string'100%'
height组件的高度. *必须带单位.string'auto'
editable是否开启可编辑功能. 如果editable的值是false, 则隐藏组件右上角的编辑开关.booleanfalse
foldable是否开启折叠功能. 如果foldable的值是false, 则unfold属性将失效, 组件将保持展开状态.booleantrue
unfold是否默认展开组件.booleanfalse
*codes需要展示的代码. 查看属性codes的格式.string | object | array[]
language组件的默认语言.string'javascript'

-- event --

名称说明回调参数回调参数类型回调参数说明
submit组件提交操作时的回调函数. 完整示例demosubmitInfoobject当前提交的内容信息和初始化编辑状态方法.

--- codes的格式 ---

类型格式示例
string-`const value = 'Hello Word!'`
object{    language:   string | 可选 | 默认: 'javascript' ,    code:   string | 可选 | 默认: '' }{    language:  'javascript',    code:  `const value = 'Hello Word!'`}
array[    {        language:   string | 可选 | 默认: 'javascript' ,        code:   string | 可选 | 默认: ''     },    ......]    {        language:  'html',        code:  `\{{ msg }}\`    },    {        language:  'javascript',        code:  `const msg = 'Hello Word!'`    },    ......

--- submitInfo ---

submitInfo的属性类型说明
dataarray需要提交的数据. [    {        code : [ 原始展示用代码 | string ],        language : [ 实际代码语言 | string ],        processedCode : [ prismjs处理过的代码 | string ],        showingLanguage : [ 展示的语言 | string ]    },    ......]
closefunction初始化组件的编辑状态为'未编辑'状态.

-- DocNoteConfig 配置 --   [完整DocNoteConfig示例]

- DocNoteConfig     * DocNoteConfig必须在组件挂载之前被调用

名称类型功能回调参数回调参数类型回调参数说明
DocNoteConfigfunction配置doc-note的主题、支持语言、权限等.ConfigureobjectDocNoteConfig方法的配置对象

--- Configure ---

Configure的属性类型功能默认值回调参数回调参数类型回调参数说明
themestring配置doc-note的主题. [所有主题]. [示例].'default'---
languagesarray配置doc-note支持的语言. [所有可被支持的语言]. [示例]['html', 'javascript', 'css']---
contentNamesobject配置doc-note中删除代码块弹框中的相关文字描述. [示例]undefined---
editConfigurefunction配置doc-note中的编辑权限. [示例]undefinednextfunction继续下一步.
addConfigurefunction配置doc-note中添加代码块权限. [示例]undefinednextfunction继续下一步.
removeConfigurefunction配置doc-note中删除代码块权限. [示例]undefinednextfunction继续下一步.
submitConfigurefunction配置doc-note中的提交权限. [示例]undefinednextfunction继续下一步.

$ - 配置示例

       - 配置主题

              - 所有支持的主题

// 本组件一共可支持8中主题, 分别是: 'default', 'coy', 'dark', 'funky', 'okaidia', 'solarizedlight', 'tomorrow', 'twilight'
// 可通过全局属性Prism.allThemes来获取所有支持的主题
console.log(Prism.allThemes) // ['default', 'coy', 'dark', 'funky', 'okaidia', 'solarizedlight', 'tomorrow', 'twilight']

              - 主题配置示例

DocNoteConfig({
  theme: 'okaidia'
})

       - 配置语言

              - 所有支持的语言

// 本组件一共可支持270+种语言
// 可通过全局属性Prism.allLanguages来获取所有支持的语言
console.log(Prism.allLanguages) // ['html', 'javascript', 'css', ...]

// 可通过全局方法Prism.hasLanguage来判断某种语言是否被支持
/**
 * 检测语言是否被支持
 * 
 * @param {String} 被检测的语言字符串
 * 
 * @returns {Boolean} 
 */
console.log(Prism.hasLanguage('html')) // true

              - 语言配置示例

DocNoteConfig({
  languages: ['html', 'javascript', 'css', 'c++', 'ASP.NET (C#)']
})

npm.io


       - 配置删除文本

DocNoteConfig({
  contentNames: {
    cancel: 'cancel', // 取消按钮展示文本
    confirm: 'done', // 确定按钮展示文本
    explain: 'Are you sure to delete??', // 删除弹框内容展示文本
  }
})

npm.io


       - 配置编辑权限

DocNoteConfig({
  // 如果不想配置edit权限, 请不要设置editConfigure, 或者在editConfigure内部直接调用next()方法
  editConfigure (next) {
    if (store.getters.isLogin) { // 如果用户已登录
      next() // 继续下一步
    } else {
      alert(`You don't have permission! Please log in first!!!`)
    }
  }
})

       - 配置添加权限

DocNoteConfig({
  // 如果不想配置add权限, 请不要设置addConfigure, 或者在addConfigure内部直接调用next()方法
  addConfigure (next) {
    if (store.getters.isLogin) { // 如果用户已登录
      next() // 继续下一步
    } else {
      alert(`You don't have permission! Please log in first!!!`)
    }
  }
})

       - 配置删除权限

DocNoteConfig({
  // 如果不想配置remove权限, 请不要设置removeConfigure, 或者在removeConfigure内部直接调用next()方法
  removeConfigure (next) {
    if (store.getters.isLogin) { // 如果用户已登录
      next() // 继续下一步
    } else {
      alert(`You don't have permission! Please log in first!!!`)
    }
  }
})

       - 配置提交权限

DocNoteConfig({
  // 如果不想配置submit权限, 请不要设置submitConfigure, 或者在submitConfigure内部直接调用next()方法
  submitConfigure (next) {
    if (store.getters.isLogin) { // 如果用户已登录
      next() // 继续下一步
    } else {
      alert(`You don't have permission! Please log in first!!!`)
    }
  }
})

       - 完整DocNoteConfig配置

DocNoteConfig({
  /**
   * 权限配置-使页面可被编辑
   * 
   * @param {Function} next 继续下一步
   */
  editConfigure (next) { // 如果不想配置edit权限, 请不要设置editConfigure, 或者在editConfigure内部直接调用next()方法
    if (store.getters.isLogin) { // 如果用户已登录
      next()
    } else {
      alert(`Oh, no! You don't have 'edit' permission! Please log in first!!!`)
    }
  },
  /**
   * 权限配置-添加代码块
   * 
   * @param {Function} next 继续下一步
   */
  addConfigure (next) { // 如果不想配置add权限, 请不要设置addConfigure, 或者在addConfigure内部直接调用next()方法
    if (store.getters.isLogin) {
      next()
    } else {
      alert(`Oh, no! You don't have 'add' permission! Please log in first!!!`)
    }
  },
  /**
   * 权限配置-删除代码块
   * 
   * @param {Function} next 继续下一步
   */
  removeConfigure (next) { // 如果不想配置remove权限, 请不要设置removeConfigure, 或者在removeConfigure内部直接调用next()方法
    if (store.getters.isLogin) {
      next()
    } else {
      alert(`Oh, no! You don't have 'remove' permission! Please log in first!!!`)
    }
  },
  /**
   * 权限配置-确认编辑的代码
   * 
   * @param {Function} next 继续下一步
   */
  submitConfigure (next) { // 如果不想配置submit权限, 请不要设置submitConfigure, 或者在submitConfigure内部直接调用next()方法
    if (store.getters.isLogin) {
      next()
    } else {
      alert(`Oh, no! You don't have 'submit' permission! Please log in first!!!`)
    }
  },
  /**
   * 配置删除代码块弹框内的文字描述
   * 
   * {Object} contentNames
   * {String} contentNames.cancel 取消按钮描述
   * {String} contentNames.confirm 确定按钮描述
   * {String} contentNames.explain 弹框内容描述
   */
  contentNames: {
    cancel: '取消',
    confirm: '确定',
    explain: '确定删除?',
  },
  /**
   * 配置添加代码块时可选择的语言
   * 
   * {Array} languages
   * 你可以通过 console.log(Prism.allLanguages) 打印出所有被支持的语言, 返回一个数组
   * 你可以通过 console.log(Prism.hasLanguage(<languageName>)) 打印出 <languageName> 语言是否被支持, 返回 true 或 false
   */
  languages: ['html', 'javascript', 'css', 'c++', 'ASP.NET (C#)'],
  /**
   * 配置主题
   * 
   * {String} theme
   * 可配置主题一共有8种: ['default', 'coy', 'dark', 'funky', 'okaidia', 'solarizedlight', 'tomorrow', 'twilight']
   */
  theme: 'default',
})

中文文档 | English Api

-- Basic Usage --

- Download dependency

npm i doc-note -S

-   Register components

// main.js
import { DocNote, DocNoteConfig } from 'doc-note'

// Umnoteconfig is the configuration method of doc-note components, Equivalent to initialization method, Must be executed before createapp(APP).use(DocNote).
DocNoteConfig()

createApp(App).use(DocNote).mount('#app')

* Umnoteconfig configuration

-   Used in .vue files

<template>
  <doc-note :codes="code"/>
</template>
import { defineComponent, ref } from 'vue'
export default defineComponent({
  setup() {
    const code = ref(`const helloWord = 'Hello, doc-note!'`)

    return {
      code,
    }
  }
})

-- props --

ParamsDescriptionTypeDefault
widthWidth of component. *Must bring unit.string'100%'
heightHeight of component. *Must bring unit.string'auto'
editableWhether to turn on the editable function. If the value of editable is false, the edit switch in the upper right corner of the component is hidden.booleanfalse
foldableWhether to turn on the folding function. If the value of foldable is false, the unfold attribute will become invalid and the component will remain unfold.booleantrue
unfoldWhether to unfold components by default.booleanfalse
*codesCode to show. View the format of attribute codes.string | object | array[]
languageThe default language of the component.string'javascript'

-- event --

NameDescriptionCB ArgumentsArg TypeArg Description
submitCallback function when component submits operation. Full sample demosubmitInfoobjectContent information currently submitted and method of initializing editing status.

--- Format of codes ---

TypeFormatExample
string-`const value = 'Hello Word!'`
object{    language:   string | Optional | Default: 'javascript' ,    code:   string | Optional | Default: '' }{    language:  'javascript',    code:  `const value = 'Hello Word!'`}
array[    {        language:   string | Optional | Default: 'javascript' ,        code:   string | Optional | Default: ''     },    ......]    {        language:  'html',        code:  `\{{ msg }}\`    },    {        language:  'javascript',        code:  `const msg = 'Hello Word!'`    },    ......

--- submitInfo ---

Props of submitinfoTypeDescription
dataarrayData to be submitted. [    {        code : [ Original display code | string ],        language : [ Actual code language | string ],        processedCode : [ Processed code by prismjs | string ],        showingLanguage : [ Language of presentation | string ]    },    ......]
closefunctionInitialize the edit state of the component to the 'not edited' state.

-- Umnoteconfig configuration --   [Complete example]

- DocNoteConfig     * Umnoteconfig must be called before the component is mounted

NameTypeDescriptionCB ArgumentsArg TypeArg Description
DocNoteConfigfunctionConfigure the theme, supported language, permissions, etc. of doc-note.ConfigureobjectConfiguration object of DocNoteConfig method

--- Configure ---

Props of ConfigureTypeDescriptionDefaultCB ArgumentsArg TypeArg Description
themestringConfigure topics for doc-note. [All themes]. [Example].'default'---
languagesarrayConfigure languages supported for 'doc-note'. [All supported languages]. [Example]['html', 'javascript', 'css']---
contentNamesobjectConfigure the relevant text description in the delete code block pop-up box for 'doc-note'. [Example]undefined---
editConfigurefunctionConfigure edit permissions for 'doc-note'. [Example]undefinednextfunctionContinue to the next step.
addConfigurefunctionConfigure add code block permissions for 'doc-note'. [Example]undefinednextfunctionContinue to the next step.
removeConfigurefunctionConfigure delete code block permission for 'doc-note'. [Example]undefinednextfunctionContinue to the next step.
submitConfigurefunctionConfigure submit permissions for 'doc-note'. [Example]undefinednextfunctionContinue to the next step.

$ - Configuration example

       - Configure theme

              - All supported themes

// doc-note supports a total of 8 themes: 'default', 'coy', 'dark', 'funky', 'okaidia', 'solarizedlight', 'tomorrow', 'twilight'
// All supported topics can be obtained through the global attribute Prism.allThemes
console.log(Prism.allThemes) // ['default', 'coy', 'dark', 'funky', 'okaidia', 'solarizedlight', 'tomorrow', 'twilight']

              - Theme configuration example

DocNoteConfig({
  theme: 'okaidia'
})

       - Configuration language

              - All supported languages

// doc-note can support 270+ languages
// All supported languages can be obtained through the global attribute Prism.allLanguages
console.log(Prism.allLanguages) // ['html', 'javascript', 'css', ...]

// The global method Prism.hasLanguage can be used to determine whether a language is supported
/**
 * Check whether the language is supported
 * 
 * @param {String} language to be detected
 * 
 * @returns {Boolean} 
 */
console.log(Prism.hasLanguage('html')) // true

              - Language configuration example

DocNoteConfig({
  languages: ['html', 'javascript', 'css', 'c++', 'ASP.NET (C#)']
})

npm.io


       - Configure delete text

DocNoteConfig({
  contentNames: {
    cancel: 'cancel', // text of cancel butto
    confirm: 'done', // text of confirm butto
    explain: 'Are you sure to delete??', // text of delete popup
  }
})

npm.io


       - Configure edit permissions

DocNoteConfig({
  // If you do not want to configure 'edit' permission, please do not set 'editconfiguration', or directly call the 'next()' method inside 'editconfiguration'
  editConfigure (next) {
    if (store.getters.isLogin) { // If the user is logged in
      next() // Continue to the next step
    } else {
      alert(`You don't have permission! Please log in first!!!`)
    }
  }
})

       - Configure add permissions

DocNoteConfig({
  // If you do not want to configure 'add' permission, please do not set 'addConfigure', or directly call the 'next()' method inside 'addConfigure'
  addConfigure (next) {
    if (store.getters.isLogin) { // If the user is logged in
      next() // Continue to the next step
    } else {
      alert(`You don't have permission! Please log in first!!!`)
    }
  }
})

       - Configure delete permissions

DocNoteConfig({
  // If you do not want to configure 'remove' permission, please do not set 'removeConfigure', or directly call the 'next()' method inside 'removeConfigure'
  removeConfigure (next) {
    if (store.getters.isLogin) { // If the user is logged in
      next() // Continue to the next step
    } else {
      alert(`You don't have permission! Please log in first!!!`)
    }
  }
})

       - Configure submit permissions

DocNoteConfig({
  // If you do not want to configure 'submit' permission, please do not set 'submitConfigure', or directly call the 'next()' method inside 'submitConfigure'
  submitConfigure (next) {
    if (store.getters.isLogin) { // If the user is logged in
      next() // Continue to the next step
    } else {
      alert(`You don't have permission! Please log in first!!!`)
    }
  }
})

       - Full DocNoteConfig configuration

DocNoteConfig({
  /**
   * Permission configuration-Make the page editable
   * 
   * @param {Function} next Continue to the next step
   */
  editConfigure (next) { // If you do not want to configure 'edit' permission, please do not set 'editconfiguration', or directly call the 'next()' method inside 'editconfiguration'
    if (store.getters.isLogin) { // If the user is logged in
      next()
    } else {
      alert(`Oh, no! You don't have 'edit' permission! Please log in first!!!`)
    }
  },
  /**
   * Permission configuration-Add code block
   * 
   * @param {Function} next Continue to the next step
   */
  addConfigure (next) { // If you do not want to configure 'add' permission, please do not set 'addConfigure', or directly call the 'next()' method inside 'addConfigure'
    if (store.getters.isLogin) {
      next()
    } else {
      alert(`Oh, no! You don't have 'add' permission! Please log in first!!!`)
    }
  },
  /**
   * Permission configuration-Delete code block
   * 
   * @param {Function} next Continue to the next step
   */
  removeConfigure (next) { // If you do not want to configure 'remove' permission, please do not set 'removeConfigure', or directly call the 'next()' method inside 'removeConfigure'
    if (store.getters.isLogin) {
      next()
    } else {
      alert(`Oh, no! You don't have 'remove' permission! Please log in first!!!`)
    }
  },
  /**
   * Permission configuration-Confirm edited code
   * 
   * @param {Function} next Continue to the next step
   */
  submitConfigure (next) { // If you do not want to configure 'submit' permission, please do not set 'submitConfigure', or directly call the 'next()' method inside 'submitConfigure'
    if (store.getters.isLogin) {
      next()
    } else {
      alert(`Oh, no! You don't have 'submit' permission! Please log in first!!!`)
    }
  },
  /**
   * Configure the text description in the delete code block pop-up box
   * 
   * {Object} contentNames
   * {String} contentNames.cancel text of cancel butto
   * {String} contentNames.confirm text of confirm butto
   * {String} contentNames.explain text of delete popup
   */
  contentNames: {
    cancel: '取消',
    confirm: '确定',
    explain: '确定删除?',
  },
  /**
   * Configure the language that can be selected when adding code blocks
   * 
   * {Array} languages
   * You can print out all supported languages through 'console.log(Prism.allLanguages)', return an array
   * You can print out whether the '<languename>' language is supported through 'console.log(Prism.hasLanguage(<languageName>))', return true or false
   */
  languages: ['html', 'javascript', 'css', 'c++', 'ASP.NET (C#)'],
  /**
   * Configure theme
   * 
   * {String} theme
   * There are 8 configurable themes: ['default', 'coy', 'dark', 'funky', 'okaidia', 'solarizedlight', 'tomorrow', 'twilight']
   */
  theme: 'default',
})

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago