mirror of
https://github.com/nextcloud/server.git
synced 2026-04-04 16:45:22 -04:00
refactor(core): migrate OCP.Comments away from jQuery
The API was not yet deprecated but is not used anymore, so 1. added a deprecation so we can get rid of it in the future. But until then we need to drop jQuery so refactored the code to use vanilla JS. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
This commit is contained in:
parent
0ff9de1a06
commit
1163638d3d
3 changed files with 30 additions and 20 deletions
|
|
@ -4,7 +4,7 @@
|
|||
*/
|
||||
|
||||
import { expect, it } from 'vitest'
|
||||
import * as Comments from '../../OCP/comments.js'
|
||||
import { plainToRich } from './comments.ts'
|
||||
|
||||
it.for([
|
||||
{ input: 'nextcloud.com', expected: 'nextcloud.com' },
|
||||
|
|
@ -28,6 +28,6 @@ it.for([
|
|||
{ input: 'FirebaseInstanceId.getInstance().deleteInstanceId()', expected: 'FirebaseInstanceId.getInstance().deleteInstanceId()' },
|
||||
{ input: 'I mean...it', expected: 'I mean...it' },
|
||||
])('OCP.Comments should parse URLs only', ({ input, expected }) => {
|
||||
const result = Comments.plainToRich(input)
|
||||
const result = plainToRich(input)
|
||||
expect(result).toEqual(expected)
|
||||
})
|
||||
|
|
@ -3,8 +3,6 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
import $ from 'jquery'
|
||||
|
||||
/*
|
||||
* Detects links:
|
||||
* Either the http(s) protocol is given or two strings, basically limited to ascii with the last
|
||||
|
|
@ -19,23 +17,29 @@ import $ from 'jquery'
|
|||
const urlRegex = /(\s|^)(https?:\/\/)([-A-Z0-9+_.]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/ig
|
||||
|
||||
/**
|
||||
* @param {any} content -
|
||||
* Converts plain text to rich text
|
||||
*
|
||||
* @param content - The plain text content
|
||||
*/
|
||||
export function plainToRich(content) {
|
||||
return this.formatLinksRich(content)
|
||||
export function plainToRich(content: string) {
|
||||
return formatLinksRich(content)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} content -
|
||||
* Converts rich text to plain text
|
||||
*
|
||||
* @param content - The rich text content
|
||||
*/
|
||||
export function richToPlain(content) {
|
||||
return this.formatLinksPlain(content)
|
||||
export function richToPlain(content: string) {
|
||||
return formatLinksPlain(content)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} content -
|
||||
* Format links in the given content to rich text links
|
||||
*
|
||||
* @param content - The content containing plain text URLs
|
||||
*/
|
||||
export function formatLinksRich(content) {
|
||||
export function formatLinksRich(content: string) {
|
||||
return content.replace(urlRegex, function(_, leadingSpace, protocol, url, trailingSpace) {
|
||||
let linkText = url
|
||||
if (!protocol) {
|
||||
|
|
@ -49,13 +53,16 @@ export function formatLinksRich(content) {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param {any} content -
|
||||
* Format links in the given content to plain text links
|
||||
*
|
||||
* @param content - The content containing rich text URLs
|
||||
*/
|
||||
export function formatLinksPlain(content) {
|
||||
const $content = $('<div></div>').html(content)
|
||||
$content.find('a').each(function() {
|
||||
const $this = $(this)
|
||||
$this.html($this.attr('href'))
|
||||
export function formatLinksPlain(content: string) {
|
||||
const el = document.createElement('div')
|
||||
el.innerHTML = content
|
||||
el.querySelectorAll('a').forEach((anchor) => {
|
||||
anchor.replaceWith(document.createTextNode(anchor.getAttribute('href') || ''))
|
||||
})
|
||||
return $content.html()
|
||||
|
||||
return el.innerHTML
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@ import { loadState } from '@nextcloud/initial-state'
|
|||
import Accessibility from './accessibility.js'
|
||||
import * as AppConfig from './appconfig.ts'
|
||||
import Collaboration from './collaboration.js'
|
||||
import * as Comments from './comments.js'
|
||||
import * as Comments from './comments.ts'
|
||||
import Loader from './loader.js'
|
||||
import Toast from './toast.js'
|
||||
import * as WhatsNew from './whatsnew.js'
|
||||
|
|
@ -17,6 +17,9 @@ export default {
|
|||
Accessibility,
|
||||
AppConfig,
|
||||
Collaboration,
|
||||
/**
|
||||
* @deprecated 33.0.0
|
||||
*/
|
||||
Comments,
|
||||
InitialState: {
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue