Skip to content

Commit

Permalink
Merge pull request #82 from coolpace/next
Browse files Browse the repository at this point in the history
Next
  • Loading branch information
Codennnn authored May 12, 2024
2 parents 3591103 + 74c25a3 commit cf9a1e9
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 133 deletions.
2 changes: 1 addition & 1 deletion extension/manifest-firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

"name": "V2EX Polish",

"version": "1.9.25",
"version": "1.10.0",

"description": "专为 V2EX 用户设计,提供了丰富的扩展功能。",

Expand Down
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "V2EX Polish",

"version": "1.9.25",
"version": "1.10.0",

"description": "专为 V2EX 用户设计,提供了丰富的扩展功能。",

Expand Down
38 changes: 20 additions & 18 deletions src/components/model.ts → src/components/modal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createButton } from './button'

interface ModelElements {
interface ModalElements {
$mask: JQuery
$main: JQuery
$container: JQuery
Expand All @@ -9,23 +9,23 @@ interface ModelElements {
$content: JQuery
}

interface ModelControl extends ModelElements {
interface ModalControl extends ModalElements {
open: () => void
close: () => void
}

interface CreateModelProps {
interface CreateModalProps {
root?: JQuery
title?: string
onMount?: (elements: ModelElements) => void
onOpen?: (elements: ModelElements) => void
onClose?: (elements: ModelElements) => void
onMount?: (elements: ModalElements) => void
onOpen?: (elements: ModalElements) => void
onClose?: (elements: ModalElements) => void
}

/**
* 创建 model 框。
* 创建 modal 框。
*/
export function createModel(props: CreateModelProps): ModelControl {
export function createModal(props: CreateModalProps): ModalControl {
const { root, title, onOpen, onClose, onMount } = props

const $mask = $('<div class="v2p-modal-mask">')
Expand All @@ -51,7 +51,7 @@ export function createModel(props: CreateModelProps): ModelControl {

const $container = $mask.append($main).hide()

const modelElements = {
const modalElements = {
$mask,
$main,
$container,
Expand All @@ -63,9 +63,11 @@ export function createModel(props: CreateModelProps): ModelControl {
// 用于判定是否已经绑定了事件, 避免重复绑定。
let boundEvent = false

const maskClickHandler = () => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
handleModalClose()
const maskClickHandler = (ev: JQuery.MouseUpEvent) => {
if (ev.currentTarget === $mask.get(0) && ev.currentTarget === ev.target) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
handleModalClose()
}
}

const keyupHandler = (ev: JQuery.KeyDownEvent) => {
Expand All @@ -76,21 +78,21 @@ export function createModel(props: CreateModelProps): ModelControl {
}

const handleModalClose = () => {
$mask.off('click', maskClickHandler)
$mask.off('mouseup', maskClickHandler)
$(document).off('keydown', keyupHandler)
boundEvent = false

$container.fadeOut('fast')
document.body.classList.remove('v2p-modal-open')

onClose?.(modelElements)
onClose?.(modalElements)
}

const handleModalOpen = () => {
// Hack: 为了防止 open 点击事件提前冒泡到 document 上,需要延迟绑定事件。
setTimeout(() => {
if (!boundEvent) {
$mask.on('click', maskClickHandler)
$mask.on('mouseup', maskClickHandler)
$(document).on('keydown', keyupHandler)
boundEvent = true
}
Expand All @@ -99,16 +101,16 @@ export function createModel(props: CreateModelProps): ModelControl {
$container.fadeIn('fast')
document.body.classList.add('v2p-modal-open')

onOpen?.(modelElements)
onOpen?.(modalElements)
}

$closeBtn.on('click', handleModalClose)

onMount?.(modelElements)
onMount?.(modalElements)

if (root) {
root.append($container)
}

return { ...modelElements, open: handleModalOpen, close: handleModalClose }
return { ...modalElements, open: handleModalOpen, close: handleModalClose }
}
2 changes: 1 addition & 1 deletion src/contents/home/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void (async () => {
const bgImg = ($cell.prop('style') as CSSStyleDeclaration).backgroundImage

if (bgImg.includes('/static/img/corner_star.png')) {
$cell.find('.count_livid').append(' | 置顶').addClass('count_orange')
$cell.find('.count_livid').append(' | 推荐').addClass('count_orange')
}
})
}
Expand Down
Loading

0 comments on commit cf9a1e9

Please sign in to comment.