-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add AyahView and ayah search utilities #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
haith2m
wants to merge
2
commits into
6km:main
Choose a base branch
from
haith2m:feat/ayah-view-and-search
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import React from 'react' | ||
| import type { Meta, StoryObj } from '@storybook/react' | ||
| import AyahView from './AyahView' | ||
| import '../fonts/index.css' | ||
| import { getAyah } from '../utils/ayah' | ||
| import type { CSSProperties } from 'react' | ||
|
|
||
| type AyahViewStoryProps = { | ||
| surah: number | ||
| ayah: number | ||
| style?: CSSProperties | ||
| className?: string | ||
| } | ||
|
|
||
| function AyahViewStory({ surah, ayah, style, className }: AyahViewStoryProps) { | ||
| const result = getAyah(surah, ayah) | ||
| if (!result) { | ||
| return <p style={{ color: '#121212' }}>Ayah not found ({surah}:{ayah})</p> | ||
| } | ||
| return <AyahView verse={result.verse} style={style} className={className} /> | ||
| } | ||
|
|
||
| const meta: Meta<typeof AyahViewStory> = { | ||
| component: AyahViewStory, | ||
| parameters: { | ||
| layout: 'centered', | ||
| }, | ||
| } | ||
|
|
||
| export default meta | ||
| type Story = StoryObj<typeof meta> | ||
|
|
||
| export const Default: Story = { | ||
| name: 'AyahView', | ||
| args: { | ||
| surah: 1, | ||
| ayah: 1, | ||
| style: { | ||
| maxWidth: '360px', | ||
| padding: '1rem', | ||
| backgroundColor: '#F3F4F5', | ||
| color: '#121212', | ||
| borderRadius: 4, | ||
| }, | ||
| }, | ||
| argTypes: { | ||
| surah: { type: 'number' }, | ||
| ayah: { type: 'number' }, | ||
| style: { control: 'object' }, | ||
| className: { type: 'string' }, | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { memo, useMemo } from 'react' | ||
| import clipboardCopy from 'clipboard-copy' | ||
| import styled from 'styled-components' | ||
|
|
||
| import QuranText from './QuranText' | ||
| import { AyahViewProps } from '../types' | ||
| import { getProcessedWordsFromVerse } from '../utils/page' | ||
| import { isArabicDigits } from '../utils/string' | ||
| import { isDeepEqual } from '../utils/array' | ||
|
|
||
| const AyahContainer = styled.div` | ||
| word-break: keep-all !important; | ||
| line-height: 1.6; | ||
| font-size: 1.1em; | ||
| direction: rtl; | ||
| text-align: right; | ||
| display: flex; | ||
| flex-wrap: wrap; | ||
| align-items: center; | ||
| gap: 0.15em; | ||
| font-family: 'quran-font_react-quran', serif; | ||
| ` | ||
|
|
||
| /** | ||
| * Handles copy text event – copies selected Quran words as space-separated text. | ||
| */ | ||
| const onQuranTextCopy = (event: React.ClipboardEvent<HTMLDivElement>) => { | ||
| const selection = document.getSelection() | ||
| const quranWordsToCopy = Array.from(document.querySelectorAll(`[data-word]`)) | ||
| .filter(node => selection?.containsNode(node, true)) | ||
| .map(wordElement => wordElement.innerHTML.trim()) | ||
| .join(' ') | ||
|
|
||
| if (typeof quranWordsToCopy === 'string' && quranWordsToCopy.length > 0) { | ||
| event.preventDefault() | ||
| clipboardCopy(quranWordsToCopy) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Renders a single ayah (verse) using the same font and word styling as ReadingView. | ||
| */ | ||
| const AyahView = memo(function AyahView({ verse, style = {}, className = '' }: AyahViewProps) { | ||
| const words = useMemo(() => getProcessedWordsFromVerse(verse), [verse]) | ||
|
|
||
| return ( | ||
| <AyahContainer | ||
| className={className} | ||
| style={style} | ||
| onCopy={onQuranTextCopy} | ||
| dir="rtl" | ||
| > | ||
| {words.map((word, wordIndex) => { | ||
| const isAyahMarker = isArabicDigits(word.text_uthmani) | ||
| const charClass = isAyahMarker ? `react-quran_ayah-marker` : 'react-quran_ayah-word' | ||
|
|
||
| return ( | ||
| <QuranText | ||
| className={charClass} | ||
| key={`ayah-word-${wordIndex}`} | ||
| text={word.text_uthmani} | ||
| data={{ | ||
| chapter: word.chapter_id, | ||
| verse: word.verse_number, | ||
| }} | ||
| /> | ||
| ) | ||
| })} | ||
| </AyahContainer> | ||
| ) | ||
| }, isDeepEqual) | ||
|
|
||
| export default AyahView | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export { default as ReadingView } from './ReadingView' | ||
| export { default as Basmalah } from '../components/Basmalah' | ||
| export { default as AyahView } from './AyahView' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export * from './components' | ||
| export { getAyah, searchAyahByText } from './utils/ayah' | ||
| export type { AyahResult, SearchAyahOptions } from './types' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import React from 'react' | ||
| import type { Meta, StoryObj } from '@storybook/react' | ||
| import AyahView from '../components/AyahView' | ||
| import '../fonts/index.css' | ||
| import { getAyah, searchAyahByText } from './ayah' | ||
|
|
||
| function AyahUtilsDemo() { | ||
| const ayah = getAyah(1, 1) | ||
| const searchResults = searchAyahByText('بسم', { limit: 5 }) | ||
|
|
||
| return ( | ||
| <div style={{ direction: 'rtl', maxWidth: 480, padding: '1rem', color: '#121212' }}> | ||
| <h3 style={{ marginTop: 0 }}>getAyah(1, 1)</h3> | ||
| {ayah ? ( | ||
| <> | ||
| <p style={{ margin: '0.5rem 0' }}> | ||
| {ayah.surahName} — آية {ayah.ayah} | ||
| </p> | ||
| <AyahView verse={ayah.verse} /> | ||
| <p style={{ fontSize: '0.85em', opacity: 0.8 }}>{ayah.text}</p> | ||
| </> | ||
| ) : ( | ||
| <p>Not found</p> | ||
| )} | ||
|
|
||
| <h3>searchAyahByText('بسم', limit: 5)</h3> | ||
| <ul style={{ paddingRight: '1.25rem', margin: 0 }}> | ||
| {searchResults.map(result => ( | ||
| <li key={`${result.surah}_${result.ayah}`} style={{ marginBottom: '0.75rem' }}> | ||
| <strong> | ||
| {result.surahName} {result.surah}:{result.ayah} | ||
| </strong> | ||
| <div style={{ fontSize: '0.9em' }}>{result.text}</div> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const meta: Meta<typeof AyahUtilsDemo> = { | ||
| component: AyahUtilsDemo, | ||
| parameters: { | ||
| layout: 'centered', | ||
| }, | ||
| } | ||
|
|
||
| export default meta | ||
| type Story = StoryObj<typeof meta> | ||
|
|
||
| export const Default: Story = { | ||
| name: 'Ayah utilities', | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import pagesData from '../data/pages-v2.json' | ||
| import { AyahResult, PageDataType, SearchAyahOptions, Verse } from '../types' | ||
| import { getChapterName } from './chapter' | ||
| import { isArabicDigits, stringToNumber, stripHarakaat } from './string' | ||
|
|
||
| let verseIndex: Map<string, Verse> | null = null | ||
|
|
||
| function buildVerseIndex(): Map<string, Verse> { | ||
| if (verseIndex) return verseIndex | ||
|
|
||
| const index = new Map<string, Verse>() | ||
| for (const page of pagesData as PageDataType) { | ||
| for (const verse of page) { | ||
| index.set(verse.d, verse) | ||
| } | ||
| } | ||
| verseIndex = index | ||
| return index | ||
| } | ||
|
|
||
| function verseToText(verse: Verse): string { | ||
| return verse.w | ||
| .filter( | ||
| ({ uthmani, type }) => | ||
| type !== 'end' && !isArabicDigits(uthmani), | ||
| ) | ||
| .map(({ uthmani }) => uthmani) | ||
| .join(' ') | ||
| } | ||
|
|
||
| function parseVerseKey(d: string): [number, number] { | ||
| const [surah, ayah] = d.split('_').map(stringToNumber) | ||
| return [surah, ayah] | ||
| } | ||
|
|
||
| function toAyahResult(verse: Verse): AyahResult { | ||
| const [surah, ayah] = parseVerseKey(verse.d) | ||
| return { | ||
| text: verseToText(verse), | ||
| surah, | ||
| surahName: getChapterName(surah), | ||
| ayah, | ||
| verse, | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns a single ayah by surah and ayah number. | ||
| */ | ||
| export function getAyah(surahNumber: number, ayahNumber: number): AyahResult | null { | ||
| const verse = buildVerseIndex().get(`${surahNumber}_${ayahNumber}`) | ||
| return verse ? toAyahResult(verse) : null | ||
| } | ||
|
|
||
| /** | ||
| * Returns ayahs whose text includes the given query (substring match). | ||
| * Matching ignores harakaat (e.g. "بسم" matches "بِسۡمِ"). | ||
| * | ||
| * Use `limit` and `offset` to paginate (e.g. page 2 with 10 per page: `{ limit: 10, offset: 10 }`). | ||
| */ | ||
| export function searchAyahByText( | ||
| query: string, | ||
| options?: SearchAyahOptions, | ||
| ): AyahResult[] { | ||
| const trimmed = query.trim() | ||
| if (!trimmed) return [] | ||
|
|
||
| const normalizedQuery = stripHarakaat(trimmed) | ||
| if (!normalizedQuery) return [] | ||
|
|
||
| const offset = Math.max(0, options?.offset ?? 0) | ||
| const limit = options?.limit | ||
| if (limit !== undefined && limit <= 0) return [] | ||
|
|
||
| const results: AyahResult[] = [] | ||
| let skipped = 0 | ||
| let done = false | ||
|
|
||
| buildVerseIndex().forEach(verse => { | ||
| if (done) return | ||
|
|
||
| const text = verseToText(verse) | ||
| if (!stripHarakaat(text).includes(normalizedQuery)) return | ||
|
|
||
| if (skipped < offset) { | ||
| skipped++ | ||
| return | ||
| } | ||
|
|
||
| results.push(toAyahResult(verse)) | ||
| if (limit !== undefined && results.length >= limit) { | ||
| done = true | ||
| } | ||
| }) | ||
|
|
||
| return results | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of receiving a Verse object, it would be better to receive two props:
surah: number
ayah: number
After that, it handles getting the Verse object and all the required data.
This ensures simpler code base and better developer experience.