Method and device for realizing display optimization of front-end prompt component
By introducing the antdUI component library in the React project, extracting the feature vectors of the target elements and expanding the display capacity, the shortcomings of the existing Tooltip components in the judgment of content visibility are solved, the display logic is optimized, and the user experience and adaptability are improved.
Patent Information
- Application Number
- CN202411533865.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-31
- Publication Date
- 2024-11-29
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The existing Tooltip components lack intelligent mechanisms in content visibility judgment, resulting in unnecessary visual interference and performance overhead, affecting user experience, especially on responsive designs and low-performance devices.
By introducing the antdUI component library in the React project, the feature vector of the target element is extracted, the display capacity value is calculated, the display capacity expansion operation is performed, and the title attribute of the Tooltip component is set to display the omitted content, ensuring that the user sees the complete content when hovering.
Optimize the display logic of Tooltip components, reduce unnecessary visual interference and performance overhead, and improve adaptability and user experience, especially on responsive designs and low-performance devices.
Smart Images

Figure CN119045690A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing technology, and in particular to a method, device and electronic device for realizing display optimization of a front-end prompt component. Background Art
[0002] The Tooltip component is a common user interface element used to display additional information or explanations when the user hovers the mouse over an element. It provides a concise way to display auxiliary text without taking up extra page space. Tooltip usually appears near the element that triggers it and disappears automatically when the mouse leaves the element.
[0003] The prior art has the following problems when using Tooltip:
[0004] 1. Unconditional triggering mechanism of Tooltip component: Current Tooltip implementations are usually triggered based on simple hover or click events, regardless of whether the content is actually omitted. This results in the Tooltip being displayed even when the content is fully visible, adding unnecessary visual interference and interaction complexity.
[0005] 2. Performance overhead and user experience impact: When there are a large number of elements that potentially require tooltips on the page (such as in a long list), attaching a tooltip listener to each element may result in performance degradation. In this case, the page response speed may slow down, affecting the overall user experience, especially on mobile devices or devices with low performance.
[0006] 3. Lack of intelligent judgment mechanism: Existing Tooltip components usually lack an effective mechanism to dynamically judge whether the content is actually omitted. This problem is particularly obvious in responsive design, because the visibility of the content may change with the change of screen size. Without this intelligent judgment, the Tooltip may be displayed when it is unnecessary, or not displayed when it is needed.
[0007] The above problems become technical problems that need to be solved. Summary of the invention
[0008] In view of this, an embodiment of the present invention provides a method, device and electronic device for realizing display optimization of a front-end prompt component, which at least partially solves the problems existing in the prior art.
[0009] In a first aspect, an embodiment of the present invention provides a method for realizing display optimization of a front-end prompt component, comprising:
[0010] In the initialization stage of the React project, introduce the antdUI component library through the npm package management tool, and configure the antdUI component library to the React project, so that the UI components in antd can be used in the React project, create the first React component, and reference the Tooltip component from antd in the first React component;
[0011] Obtain the content C to be displayed contained in the target element, perform feature extraction on the content C to be displayed, and obtain a first feature vector K1=[Ld, Fs, Cw, Lh] and a second feature vector K2=[N, Pd, Bd, Ls, Fw], where Ld represents the total text length of the content C to be displayed, Fs represents the font size of the content C to be displayed, Cw represents the average character width of the content C to be displayed, Lh represents the average character line height of the content C to be displayed, N represents the maximum number of lines allowed for the content C to be displayed, Pd represents the inner margin of the content C to be displayed, Bd represents the border width of the content C to be displayed, Ls represents the line spacing of the content C to be displayed, and Fw represents the font thickness value of the content C to be displayed;
[0012] Calculate the display capacity value R0 of the target element based on the current actual width W1 and actual height H1 of the target element;
[0013] When the display capacity value R0 is greater than R1 and less than R1*R2, the display area of the target element is expanded to obtain the expanded width W2 and expanded height H2 of the target element, [W2, H2] = Gk (W1, H1, R0, R1, R2), where R1 is the first eigenvalue of the first eigenvector K1, R2 is the second eigenvalue of the second eigenvector K2, and Gk is the expansion calculation function;
[0014] After the expansion operation is completed, the omitted content corresponding to the content C to be displayed in the target element is calculated based on the expansion width W2 and the expansion height H2, and the title attribute of the Tooltip component is set to the omitted content, so that the user can see the complete content C to be displayed when the mouse hovers over the target element.
[0015] According to a specific implementation of the embodiment of the present disclosure, the reference of the Tooltip component from antd in the first React component includes:
[0016] After the Tooltip component is imported separately from the antd library, the target element to which the prompt information is added for the Tooltip component is determined, and the target element is wrapped with the imported Tooltip component.
[0017] According to a specific implementation of the embodiment of the present disclosure, the acquiring of the content C to be displayed contained in the target element, and feature extraction of the content C to be displayed to obtain a first feature vector K1=[Ld, Fs, Cw, Lh] and a second feature vector K2=[N, Pd, Bd, Ls, Fw] include:
[0018] By accessing the textContent attribute of the target element, the content C to be displayed is extracted from the target element;
[0019] Count the number of characters in the extracted text content C, and use the number of characters as the component Ld of the feature vector K1;
[0020] Use the CSSOM interface provided by the browser to read the font size Fs currently applied to the target element;
[0021] Create a hidden temporary element, fill it with single characters and measure its width, then multiply it by the text length to calculate the average character width Cw;
[0022] Measure the average character line height Lh by reading the line-height CSS property of the target element.
[0023] According to a specific implementation of the embodiment of the present disclosure, the step of acquiring the content C to be displayed contained in the target element, performing feature extraction on the content C to be displayed, and obtaining a first feature vector K1=[Ld, Fs, Cw, Lh] and a second feature vector K2=[N, Pd, Bd, Ls, Fw] also includes:
[0024] According to the actual height of the element and the line height, calculate the maximum number of lines that the text can be displayed without being truncated, and get the maximum number of lines allowed N;
[0025] Use the getComputedStyle method in combination with the padding-top, padding-right, padding-bottom, and padding-left properties to get the padding value Pd between the target element content and its border;
[0026] Use the getComputedStyle method to read the border-width attribute of the target element to obtain the border width Bd;
[0027] Calculate the line spacing Ls by checking the difference between the element’s line-height CSS property and the font size Fs;
[0028] The font-weight attribute of the target element is read through the getComputedStyle method to obtain the font weight value Fw.
[0029] According to a specific implementation of the embodiment of the present disclosure, the calculation of the display capacity value R0 of the target element based on the current actual width W1 and actual height H1 of the target element includes:
[0030] Based on the average character width Cw, determine the average pixel width occupied by each character;
[0031] Calculate the effective content width W3=W1–2Pd;
[0032] Based on the average character width Cw and the effective content width W3, calculate the number of characters N1 that each line can accommodate:
[0033] .
[0034] According to a specific implementation of the embodiment of the present disclosure, the calculation of the display capacity value R0 of the target element based on the current actual width W1 and actual height H1 of the target element also includes:
[0035] Calculate the effective content height H3=H1-2(Pd+Bd);
[0036] Calculate the number of rows N2 that can be accommodated:
[0037]
[0038] Based on the number of characters that can be accommodated in each line and the number of lines that can be accommodated, the display capacity value R0 is obtained:
[0039] .
[0040] According to a specific implementation of the embodiment of the present disclosure, the display expansion operation is performed on the display area of the target element to obtain the expansion width W2 and the expansion height H2 of the target element, including:
[0041] The expanded width W2 is calculated based on the average character width Cw and the maximum number of characters per line:
[0042]
[0043] Where MCL is the maximum number of characters that can be contained in each line.
[0044] According to a specific implementation of the embodiment of the present disclosure, the display expansion operation is performed on the display area of the target element to obtain the expansion width W2 and the expansion height H2 of the target element, and further includes:
[0045] Calculate the expansion height H2 based on the row height Lh and the maximum number of rows N:
[0046] .
[0047] In a second aspect, an embodiment of the present invention provides a device for implementing display optimization of a front-end prompt component, including:
[0048] Initial module, in the initialization stage of the React project, introduce the antdUI component library through the npm package management tool, and configure the antdUI component library to the React project, so that the UI components in antd can be used in the React project, create the first React component, and reference the Tooltip component from antd in the first React component;
[0049] an extraction module, obtaining the content C to be displayed contained in the target element, performing feature extraction on the content C to be displayed, and obtaining a first feature vector K1=[Ld, Fs, Cw, Lh] and a second feature vector K2=[N, Pd, Bd, Ls, Fw], where Ld represents the total text length of the content C to be displayed, Fs represents the font size of the content C to be displayed, Cw represents the average character width of the content C to be displayed, Lh represents the average character line height of the content C to be displayed, N represents the maximum number of lines allowed for the content C to be displayed, Pd represents the inner margin of the content C to be displayed, Bd represents the border width of the content C to be displayed, Ls represents the line spacing of the content C to be displayed, and Fw represents the font thickness value of the content C to be displayed;
[0050] A calculation module, which calculates a display capacity value R0 of the target element based on a current actual width W1 and an actual height H1 of the target element;
[0051] An expansion module, when the display capacity value R0 is greater than R1 and less than R1*R2, performs a display expansion operation on the display area of the target element to obtain the expansion width W2 and expansion height H2 of the target element, [W2, H2]=Gk(W1, H1, R0, R1, R2), wherein R1 is the first eigenvalue of the first eigenvector K1, R2 is the second eigenvalue of the second eigenvector K2, and Gk is an expansion calculation function;
[0052] After the expansion operation is completed, the display module calculates the omitted content corresponding to the content C to be displayed in the target element based on the expansion width W2 and the expansion height H2, and sets the title attribute of the Tooltip component to the omitted content, so that the user can see the complete content C to be displayed when the mouse hovers over the target element.
[0053] In a third aspect, an embodiment of the present invention further provides an electronic device, the electronic device comprising:
[0054] at least one processor; and,
[0055] a memory communicatively connected to the at least one processor; wherein,
[0056] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the implementation method of optimizing the display of the front-end prompt component in any of the first aspects or any implementation methods of the first aspect.
[0057] In a fourth aspect, an embodiment of the present invention further provides a non-transitory computer-readable storage medium, which stores computer instructions, and the computer instructions are used to enable the computer to execute the implementation method of optimizing the display of the front-end prompt component in the aforementioned first aspect or any implementation method of the first aspect.
[0058] In the fifth aspect, an embodiment of the present invention further provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer executes the implementation method of optimizing the display of the front-end prompt component in the aforementioned first aspect or any implementation method of the first aspect.
[0059] The implementation scheme of the front-end prompt component display optimization in the embodiment of the present invention includes: in the initialization stage of the React project, introducing the antdUI component library through the npm package management tool, and configuring the antdUI component library into the React project, so that the UI component in antd can be used in the React project, creating a first React component, and referencing the Tooltip component from antd in the first React component; obtaining the content C to be displayed contained in the target element, performing feature extraction on the content C to be displayed, and obtaining a first feature vector K1=[Ld, Fs, Cw, Lh] and a second feature vector K2=[N, Pd, Bd, Ls, Fw], where Ld represents the total text length of the content C to be displayed, Fs represents the font size of the content C to be displayed, Cw represents the average character width of the content C to be displayed, Lh represents the average character line height of the content C to be displayed, N represents the maximum number of lines allowed for the content C to be displayed, Pd represents the inner margin of the content C to be displayed, and Bd represents the inner margin of the content C to be displayed. d represents the border width of the content C to be displayed, Ls represents the line spacing of the content C to be displayed, and Fw represents the font thickness value of the content C to be displayed; based on the current actual width W1 and actual height H1 of the target element, the display capacity value R0 of the target element is calculated; when the display capacity value R0 is greater than R1 and less than R1*R2, the display area of the target element is expanded to obtain the expanded width W2 and expanded height H2 of the target element, [W2, H2]=Gk(W1, H1, R0, R1, R2), wherein R1 is the first eigenvalue of the first eigenvector K1, R2 is the second eigenvalue of the second eigenvector K2, and Gk is the expansion calculation function; after the expansion operation is completed, the omitted content corresponding to the content C to be displayed in the target element is calculated based on the expanded width W2 and the expanded height H2, and the title attribute of the Tooltip component is set to the omitted content, so that the user can see the complete content C to be displayed when the mouse hovers over the target element. Through the solution of the present application, dynamic content detection is used to optimize the logic of whether the Tooltip component is displayed, thereby improving the adaptability of the Tooltip component in different application scenarios. BRIEF DESCRIPTION OF THE DRAWINGS
[0060] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0061] Figure 1 A schematic flow chart of a method for implementing front-end prompt component display optimization provided by an embodiment of the present invention;
[0062] Figure 2 A schematic flow chart of another method for implementing the optimization of the display of a front-end prompt component provided by an embodiment of the present invention;
[0063] Figure 3 A schematic diagram of the structure of a device for realizing optimization of display of a front-end prompt component provided by an embodiment of the present invention;
[0064] Figure 4 A schematic diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0065] The embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0066] The following describes the embodiments of the present disclosure through specific examples, and those skilled in the art can easily understand other advantages and effects of the present disclosure from the contents disclosed in this specification. Obviously, the described embodiments are only a part of the embodiments of the present disclosure, rather than all of the embodiments. The present disclosure can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present disclosure. It should be noted that the following embodiments and features in the embodiments can be combined with each other without conflict. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present disclosure.
[0067] It should be noted that various aspects of the embodiments within the scope of the appended claims are described below. It should be apparent that the aspects described herein may be embodied in a wide variety of forms, and any specific structure and / or function described herein is merely illustrative. Based on the present disclosure, it should be understood by those skilled in the art that an aspect described herein may be implemented independently of any other aspect, and two or more of these aspects may be combined in various ways. For example, any number of aspects described herein may be used to implement the device and / or practice the method. In addition, other structures and / or functionalities other than one or more of the aspects described herein may be used to implement this device and / or practice this method.
[0068] It should also be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present disclosure. The drawings only show components related to the present disclosure rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.
[0069] Additionally, in the following description, specific details are provided to facilitate a thorough understanding of the examples. However, it will be understood by those skilled in the art that the aspects described may be practiced without these specific details.
[0070] The embodiment of the present disclosure provides a method for implementing the optimization of the display of the front-end prompt component. The method for implementing the optimization of the display of the front-end prompt component provided in this embodiment can be executed by a computing device, which can be implemented as software, or as a combination of software and hardware, and the computing device can be integrated in a server, terminal device, etc.
[0071] See also Figure 1 and Figure 2 The embodiment of the present disclosure provides a method for realizing display optimization of a front-end prompt component, including:
[0072] S101, in the initialization stage of the React project, the antdUI component library is introduced through the npm package management tool, and the antdUI component library is configured into the React project, so that the UI components in antd can be used in the React project, a first React component is created, and a Tooltip component from antd is referenced in the first React component.
[0073] In the initialization stage of the React project, the AntDesign (antd) UI component library is introduced through the npm package management tool and configured into the project, so that the React application can quickly have rich and high-quality UI components.
[0074] First, you can use npm or yarn to install the antd library. This step can be performed in the command line, making sure the project has been initialized and has a package.json file.
[0075] In order to make antd's style effective, you can import antd's CSS file into the project. You can choose to import it globally or load it on demand. Global import is achieved by importing antd / dist / antd.css in the entry file (such as index.js or App.js). On-demand loading can be achieved by configuring build tools such as Webpack, which can reduce the size of the final package file.
[0076] Next, create a new React functional component or class component. This component will be the basis for using antd components. You can add any basic content and logic in this component.
[0077] In the newly created React component, import the Tooltip component from the antd library. Tooltip is used to display additional information when the user hovers over an element. You can define the tooltip content to be displayed by setting the title attribute.
[0078] Identify the target element to which you want to add tooltip information. The target element can be a button, icon, or other element that requires additional explanation. Then, wrap the target element with the Tooltip component. Tooltip provides a variety of properties to control its behavior and appearance, such as the placement property, which can be used to specify where the tooltip box appears.
[0079] You can further configure Tooltip properties based on your needs. For example, you can set overlayStyle to customize the style of the tooltip, or use mouseEnterDelay and mouseLeaveDelay to control the delay time for showing and hiding the tooltip.
[0080] Through the above steps, you can introduce and use antd's Tooltip component in your React project. This not only improves the user's interactive experience, but also simplifies the development process, allowing you to focus on the implementation of business logic.
[0081] S102, obtaining the content C to be displayed contained in the target element, performing feature extraction on the content C to be displayed, and obtaining a first feature vector K1=[Ld, Fs, Cw, Lh] and a second feature vector K2=[N, Pd, Bd, Ls, Fw], where Ld represents the total text length of the content C to be displayed, Fs represents the font size of the content C to be displayed, Cw represents the average character width of the content C to be displayed, Lh represents the average character line height of the content C to be displayed, N represents the maximum number of lines allowed for the content C to be displayed, Pd represents the inner margin of the content C to be displayed, Bd represents the border width of the content C to be displayed, Ls represents the line spacing of the content C to be displayed, and Fw represents the font weight value of the content C to be displayed.
[0082] In the process of obtaining the content C to be displayed contained in the target element and performing feature extraction on it to obtain the first feature vector K1=[Ld, Fs, Cw, Lh] and the second feature vector K2=[N, Pd, Bd, Ls, Fw], the following steps may be followed:
[0083] S1021. Obtain content C to be displayed.
[0084] First, identify the target element for feature extraction. This usually refers to a text container on the page, such as 、 Or other HTML elements that can contain text. Extracting the content C to be displayed from the element can be done by accessing the textContent or innerText property of the DOM element to obtain the content in plain text.
[0085] S1022. Extracting the first eigenvector K1, including extracting the following content:
[0086] Total text length (Ld): counts the number of characters in the extracted text content C, including spaces and special characters.
[0087] Font size (Fs): Read the font size currently applied to the target element through the CSS style sheet or JavaScript method. If the font size is in relative units (such as em or rem), it may need to be converted to absolute units (such as pixels px).
[0088] Average Character Width (Cw): Estimate the average width of each character in the text. One way is to create a hidden temporary element, fill it with single characters and measure its width, then multiply it by the text length to get a rough estimate. Another more accurate method is to use the canvas API or other graphics libraries to draw characters one by one and measure their actual width.
[0089] Average character line height (Lh): This value can be obtained directly by reading the line-height CSS property of the element. If the line-height is given as a multiple, it needs to be multiplied by the font size to get the actual line height value.
[0090] S1023. Extracting the second eigenvector K2, including extracting the following content:
[0091] Maximum number of lines allowed (N): Determines the maximum number of lines that can be displayed without being truncated. This may be based on the design specification or calculated based on the actual height of the element and the line height. If there is a fixed height limit, the maximum number of lines can be estimated by dividing this height by the line height (Lh).
[0092] Padding (Pd): Get the padding value of the target element through CSS style or JavaScript. Padding refers to the space between the element content and its border. You can use the getComputedStyle method to combine the padding-top, padding-right, padding-bottom, and padding-left properties to get these values, and then calculate the total padding.
[0093] Border width (Bd): Similarly, use the getComputedStyle method to read the border-width property of the target element to obtain the total width of the border. If the border has four different widths, you need to read border-top-width, border-right-width, border-bottom-width, and border-left-width separately and calculate their sum.
[0094] Line spacing (Ls): Line spacing is the extra white space between two lines of text. This can be calculated by inspecting the difference between the element's line-height CSS property and the font size (Fs). If line-height is given as a multiple, it needs to be converted to a specific pixel value first.
[0095] Font weight value (Fw): Read the font-weight property of the target element through the getComputedStyle method. The font weight value is usually a number (such as 400 for regular and 700 for bold), but it can also be a keyword (such as bold). If it is a keyword, it may need to be converted into a corresponding numerical representation.
[0096] Through the above steps, the text content in the target element can be fully analyzed, thus providing accurate data support for subsequent layout adjustments. These feature vectors help to dynamically adjust the size of the element to ensure that the content can be displayed completely and beautifully.
[0097] S103: Calculate the display capacity value R0 of the target element based on the current actual width W1 and actual height H1 of the target element.
[0098] Based on the current actual width W1 and actual height H1 of the target element, the process of calculating the display capacity value R0 involves evaluating the layout capabilities of the text content within a given space. This process needs to take into account multiple factors, including font size, line height, inner margin, border width, etc.
[0099] The basic parameters determined include:
[0100] Actual width (W1): The current actual width of the target element.
[0101] Actual Height (H1): The current actual height of the target element.
[0102] Font Size (Fs): The font size of the text in the target element.
[0103] Average character width (Cw): The average pixel width of each character.
[0104] Line height (Lh): The distance from the baseline of a line of text to the baseline of the next line.
[0105] Padding (Pd): The padding of the target element, including top, right, bottom, and left directions.
[0106] As an example, the average pixel width occupied by each character can be determined based on the average character width Cw; the effective content width W3=W1-2*Pd is calculated; based on the average character width Cw and the effective content width W3, the number of characters N1 that can be accommodated in each line is calculated:
[0107] .
[0108] Furthermore, the effective content height H3 = H1-2(Pd+Bd) can be calculated;
[0109] Calculate the number of rows N2 that can be accommodated:
[0110]
[0111] Based on the number of characters that can be accommodated in each line and the number of lines that can be accommodated, the display capacity value R0 is obtained:
[0112] .
[0113] S104, when the display capacity value R0 is greater than R1 and less than R1*R2, the display area of the target element is expanded to obtain the expanded width W2 and expanded height H2 of the target element, [W2, H2]=Gk(W1, H1, R0, R1, R2), wherein R1 is the first eigenvalue of the first eigenvector K1, R2 is the second eigenvalue of the second eigenvector K2, and Gk is the expansion calculation function.
[0114] When the display capacity value R0 is greater than R1 and less than R1 * R2, it means that the current target element size is not enough to fully display the content C, but it does not need to be significantly expanded. In this case, the display area of the target element can be appropriately expanded to ensure the effect of content display.
[0115] The expansion calculation function Gk can be constructed to calculate the new width W2 and height H2 according to the current width W1, height H1, display capacity value R0 and eigenvalues R1 and R2. The eigenvalues R1 and R2 can be obtained by selecting one or more elements in the eigenvectors K1 and K2.
[0116] As an example, the expanded width W2 can be calculated based on the average character width Cw and the maximum number of characters per line:
[0117]
[0118] Where MCL is the maximum number of characters that can be contained in each line.
[0119] Furthermore, the expansion height H2 can be calculated based on the row height Lh and the maximum number of rows N:
[0120]
[0121] Apply the calculated new width W2 and new height H2 to the target element. This can be done by dynamically modifying the element's CSS style.
[0122] Through the above steps, the new width W2 and height H2 can be calculated according to the display capacity value R0 and the eigenvalues of the eigenvectors K1 and K2, and appropriate expansion operations can be performed to ensure that the content can be fully displayed.
[0123] S105, after the expansion operation is completed, the omitted content corresponding to the content C to be displayed in the target element is calculated based on the expansion width W2 and the expansion height H2, and the title attribute of the Tooltip component is set to the omitted content, so that the user can see the complete content C to be displayed when the user hovers the mouse over the target element.
[0124] After the expansion operation is completed, if the size of the target element (W2, H2) is still not enough to fully display the content C to be displayed, the excess content can be regarded as omitted content, and the title attribute of the Tooltip component can be set to these omitted contents. In this way, when the user hovers the mouse over the target element, the complete content to be displayed can be seen through the Tooltip. The following are the specific steps:
[0125] 1. Calculate the new display capacity value
[0126] Based on the new width W2 and height H2, the display capacity value R0' of the target element is recalculated. If the new display capacity value R0' is still less than the total length Ld of the text, it means that even if the capacity is expanded, the content cannot be fully displayed and needs to be omitted.
[0127] The maximum number of characters that can be displayed can be calculated based on the new display capacity value R0'.
[0128]
[0129] Extract the first VisibleChars characters from the content C to be displayed.
[0130]
[0131] The remaining part is the omitted content OC.
[0132]
[0133] Set the omitted content as the title property of the Tooltip component so that users can see the full content when hovering.
[0134] Sets the visible content VisibleContent to the target element's display content and adds an ellipsis (...) to indicate that the content is truncated.
[0135] Through the above steps, it can be ensured that after the expansion, if the content still cannot be fully displayed, the Tooltip component can be used to provide users with the complete content to be displayed. This not only maintains the beauty of the page layout, but also provides a good user experience.
[0136] According to a specific implementation of the embodiment of the present disclosure, the reference of the Tooltip component from antd in the first React component includes:
[0137] After the Tooltip component is imported separately from the antd library, the target element to which the prompt information is added for the Tooltip component is determined, and the target element is wrapped with the imported Tooltip component.
[0138] According to a specific implementation of the embodiment of the present disclosure, the acquiring of the content C to be displayed contained in the target element, and feature extraction of the content C to be displayed to obtain a first feature vector K1=[Ld, Fs, Cw, Lh] and a second feature vector K2=[N, Pd, Bd, Ls, Fw] include:
[0139] By accessing the textContent attribute of the target element, the content C to be displayed is extracted from the target element;
[0140] Count the number of characters in the extracted text content C, and use the number of characters as the component Ld of the feature vector K1;
[0141] Use the CSSOM interface provided by the browser to read the font size Fs currently applied to the target element;
[0142] Create a hidden temporary element, fill it with single characters and measure its width, then multiply it by the text length to calculate the average character width Cw;
[0143] Measure the average character line height Lh by reading the line-height CSS property of the target element.
[0144] According to a specific implementation of the embodiment of the present disclosure, the step of acquiring the content C to be displayed contained in the target element, performing feature extraction on the content C to be displayed, and obtaining a first feature vector K1=[Ld, Fs, Cw, Lh] and a second feature vector K2=[N, Pd, Bd, Ls, Fw] also includes:
[0145] According to the actual height of the element and the line height, calculate the maximum number of lines that the text can be displayed without being truncated, and get the maximum number of lines allowed N;
[0146] Use the getComputedStyle method in combination with the padding-top, padding-right, padding-bottom, and padding-left properties to get the padding value Pd between the target element content and its border;
[0147] Use the getComputedStyle method to read the border-width attribute of the target element to obtain the border width Bd;
[0148] Calculate the line spacing Ls by checking the difference between the element’s line-height CSS property and the font size Fs;
[0149] The font-weight attribute of the target element is read through the getComputedStyle method to obtain the font weight value Fw.
[0150] According to a specific implementation of the embodiment of the present disclosure, the calculation of the display capacity value R0 of the target element based on the current actual width W1 and actual height H1 of the target element includes:
[0151] Based on the average character width Cw, determine the average pixel width occupied by each character;
[0152] Calculate the effective content width W3=W1–2Pd;
[0153] Based on the average character width Cw and the effective content width W3, calculate the number of characters N1 that each line can accommodate:
[0154] .
[0155] According to a specific implementation of the embodiment of the present disclosure, the calculation of the display capacity value R0 of the target element based on the current actual width W1 and actual height H1 of the target element also includes:
[0156] Calculate the effective content height H3=H1-2(Pd+Bd);
[0157] Calculate the number of rows N2 that can be accommodated:
[0158]
[0159] Based on the number of characters that can be accommodated in each line and the number of lines that can be accommodated, the display capacity value R0 is obtained:
[0160] .
[0161] According to a specific implementation of the embodiment of the present disclosure, the display expansion operation is performed on the display area of the target element to obtain the expansion width W2 and the expansion height H2 of the target element, including:
[0162] The expanded width W2 is calculated based on the average character width Cw and the maximum number of characters per line:
[0163]
[0164] Where MCL is the maximum number of characters that can be contained in each line.
[0165] According to a specific implementation of the embodiment of the present disclosure, the display expansion operation is performed on the display area of the target element to obtain the expansion width W2 and the expansion height H2 of the target element, and further includes:
[0166] Calculate the expansion height H2 based on the row height Lh and the maximum number of rows N:
[0167] .
[0168] Corresponding to the above method embodiment, see Figure 3 The embodiment of the present invention further discloses a device 30 for realizing optimization of display of a front-end prompt component, comprising:
[0169] Initial module 301, in the initialization stage of the React project, introduce the antdUI component library through the npm package management tool, and configure the antdUI component library into the React project, so that the UI components in antd can be used in the React project, create a first React component, and reference the Tooltip component from antd in the first React component;
[0170] The extraction module 302 obtains the content C to be displayed contained in the target element, performs feature extraction on the content C to be displayed, and obtains a first feature vector K1=[Ld, Fs, Cw, Lh] and a second feature vector K2=[N, Pd, Bd, Ls, Fw], where Ld represents the total text length of the content C to be displayed, Fs represents the font size of the content C to be displayed, Cw represents the average character width of the content C to be displayed, Lh represents the average character line height of the content C to be displayed, N represents the maximum number of lines allowed for the content C to be displayed, Pd represents the inner margin of the content C to be displayed, Bd represents the border width of the content C to be displayed, Ls represents the line spacing of the content C to be displayed, and Fw represents the font thickness value of the content C to be displayed;
[0171] The calculation module 303 calculates the display capacity value R0 of the target element based on the current actual width W1 and actual height H1 of the target element;
[0172] The expansion module 304 performs a display expansion operation on the display area of the target element when the display capacity value R0 is greater than R1 and less than R1*R2 to obtain the expansion width W2 and expansion height H2 of the target element, [W2, H2]=Gk(W1, H1, R0, R1, R2), wherein R1 is the first eigenvalue of the first eigenvector K1, R2 is the second eigenvalue of the second eigenvector K2, and Gk is an expansion calculation function;
[0173] After the expansion operation is completed, the display module 305 calculates the omitted content corresponding to the content C to be displayed in the target element based on the expansion width W2 and the expansion height H2, and sets the title attribute of the Tooltip component to the omitted content, so that the user can see the complete content C to be displayed when the mouse hovers over the target element.
[0174] See also Figure 4 The embodiment of the present invention further provides an electronic device 60, the electronic device comprising:
[0175] at least one processor; and,
[0176] a memory communicatively connected to the at least one processor; wherein,
[0177] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the implementation method of the front-end prompt component display optimization in the aforementioned method embodiment.
[0178] An embodiment of the present invention further provides a non-transitory computer-readable storage medium, which stores computer instructions, and the computer instructions are used to enable the computer to execute the aforementioned method embodiment.
[0179] An embodiment of the present invention also provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium. The computer program includes program instructions. When the program instructions are executed by a computer, the computer executes the implementation method of the front-end prompt component display optimization in the aforementioned method embodiment.
[0180] Reference below Figure 4 , which shows a schematic diagram of the structure of an electronic device 60 suitable for implementing the embodiment of the present disclosure. The electronic device in the embodiment of the present disclosure may include, but is not limited to, mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle terminals (such as vehicle navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 4 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0181] like Figure 4 As shown, the electronic device 60 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the electronic device 60 are also stored. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0182] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, an image sensor, a microphone, a gage, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 608 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 609. The communication device 609 may allow the electronic device 60 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 4 The electronic device 60 is shown with various devices, but it should be understood that it is not required to implement or have all the devices shown. More or fewer devices may be implemented or have instead.
[0183] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device 609, or installed from a storage device 608, or installed from a ROM 602. When the computer program is executed by the processing device 601, the above-mentioned functions defined in the method of the embodiment of the present disclosure are executed.
[0184] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed by the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.
Claims
1. A method for realizing display optimization of a front-end prompt component, characterized in that: include: In the initialization stage of the React project, introduce the antdUI component library through the npm package management tool, and configure the antdUI component library to the React project, so that the UI components in antd can be used in the React project, create the first React component, and reference the Tooltip component from antd in the first React component; Obtain the content C to be displayed contained in the target element, perform feature extraction on the content C to be displayed, and obtain a first feature vector K1=[Ld, Fs, Cw, Lh] and a second feature vector K2=[N, Pd, Bd, Ls, Fw], where Ld represents the total text length of the content C to be displayed, Fs represents the font size of the content C to be displayed, Cw represents the average character width of the content C to be displayed, Lh represents the average character line height of the content C to be displayed, N represents the maximum number of lines allowed for the content C to be displayed, Pd represents the inner margin of the content C to be displayed, Bd represents the border width of the content C to be displayed, Ls represents the line spacing of the content C to be displayed, and Fw represents the font thickness value of the content C to be displayed; Calculate the display capacity value R0 of the target element based on the current actual width W1 and actual height H1 of the target element; When the display capacity value R0 is greater than R1 and less than R1*R2, the display area of the target element is expanded to obtain the expanded width W2 and expanded height H2 of the target element, [W2, H2] = Gk (W1, H1, R0, R1, R2), where R1 is the first eigenvalue of the first eigenvector K1, R2 is the second eigenvalue of the second eigenvector K2, and Gk is the expansion calculation function; After the expansion operation is completed, the omitted content corresponding to the content C to be displayed in the target element is calculated based on the expansion width W2 and the expansion height H2, and the title attribute of the Tooltip component is set to the omitted content, so that the user can see the complete content C to be displayed when the mouse hovers over the target element.
2. The method according to claim 1, characterized in that The Tooltip component from antd is referenced in the first React component, including: After the Tooltip component is imported separately from the antd library, the target element to which the prompt information is added for the Tooltip component is determined, and the target element is wrapped with the imported Tooltip component.
3. The method according to claim 2, characterized in that The acquiring the content C to be displayed contained in the target element, performing feature extraction on the content C to be displayed, and obtaining a first feature vector K1=[Ld, Fs, Cw, Lh] and a second feature vector K2=[N, Pd, Bd, Ls, Fw], includes: By accessing the textContent attribute of the target element, the content C to be displayed is extracted from the target element; Count the number of characters in the extracted text content C, and use the number of characters as the component Ld of the feature vector K1; Use the CSSOM interface provided by the browser to read the font size Fs currently applied to the target element; Create a hidden temporary element, fill it with single characters and measure its width, then multiply it by the text length to calculate the average character width Cw; Measure the average character line height Lh by reading the line-height CSS property of the target element.
4. The method according to claim 3, characterized in that The step of acquiring the content C to be displayed contained in the target element, extracting features of the content C to be displayed, and obtaining a first feature vector K1=[Ld, Fs, Cw, Lh] and a second feature vector K2=[N, Pd, Bd, Ls, Fw] also includes: According to the actual height of the element and the line height, calculate the maximum number of lines that the text can be displayed without being truncated, and get the maximum number of lines allowed N; Use the getComputedStyle method in combination with the padding-top, padding-right, padding-bottom, and padding-left properties to get the padding value Pd between the target element content and its border; Use the getComputedStyle method to read the border-width attribute of the target element to obtain the border width Bd; Calculate the line spacing Ls by checking the difference between the element’s line-height CSS property and the font size Fs; The font-weight attribute of the target element is read through the getComputedStyle method to obtain the font weight value Fw.
5. The method according to claim 4, characterized in that The calculating the display capacity value R0 of the target element based on the current actual width W1 and actual height H1 of the target element includes: Based on the average character width Cw, determine the average pixel width occupied by each character; Calculate the effective content width W3=W1–2Pd; Based on the average character width Cw and the effective content width W3, calculate the number of characters N1 that each line can accommodate: 。 6. The method according to claim 5, characterized in that The step of calculating the display capacity value R0 of the target element based on the current actual width W1 and actual height H1 of the target element further includes: Calculate the effective content height H3=H1-2(Pd+Bd); Calculate the number of rows N2 that can be accommodated: Based on the number of characters that can be accommodated in each line and the number of lines that can be accommodated, the display capacity value R0 is obtained: 。 7. The method according to claim 6, characterized in that The performing a display expansion operation on the display area of the target element to obtain an expansion width W2 and an expansion height H2 of the target element includes: The expanded width W2 is calculated based on the average character width Cw and the maximum number of characters per line: Where MCL is the maximum number of characters that can be contained in each line.
8. The method according to claim 7, characterized in that The step of performing a display expansion operation on the display area of the target element to obtain an expansion width W2 and an expansion height H2 of the target element further includes: Calculate the expansion height H2 based on the row height Lh and the maximum number of rows N: 。 9. A device for realizing display optimization of a front-end prompt component, characterized in that: include: Initial module, in the initialization stage of the React project, introduce the antdUI component library through the npm package management tool, and configure the antdUI component library to the React project, so that the UI components in antd can be used in the React project, create the first React component, and reference the Tooltip component from antd in the first React component; an extraction module, obtaining the content C to be displayed contained in the target element, performing feature extraction on the content C to be displayed, and obtaining a first feature vector K1=[Ld, Fs, Cw, Lh] and a second feature vector K2=[N, Pd, Bd, Ls, Fw], where Ld represents the total text length of the content C to be displayed, Fs represents the font size of the content C to be displayed, Cw represents the average character width of the content C to be displayed, Lh represents the average character line height of the content C to be displayed, N represents the maximum number of lines allowed for the content C to be displayed, Pd represents the inner margin of the content C to be displayed, Bd represents the border width of the content C to be displayed, Ls represents the line spacing of the content C to be displayed, and Fw represents the font thickness value of the content C to be displayed; A calculation module, which calculates a display capacity value R0 of the target element based on a current actual width W1 and an actual height H1 of the target element; An expansion module, when the display capacity value R0 is greater than R1 and less than R1*R2, performs a display expansion operation on the display area of the target element to obtain the expansion width W2 and expansion height H2 of the target element, [W2, H2]=Gk(W1, H1, R0, R1, R2), wherein R1 is the first eigenvalue of the first eigenvector K1, R2 is the second eigenvalue of the second eigenvector K2, and Gk is an expansion calculation function; After the expansion operation is completed, the display module calculates the omitted content corresponding to the content C to be displayed in the target element based on the expansion width W2 and the expansion height H2, and sets the title attribute of the Tooltip component to the omitted content, so that the user can see the complete content C to be displayed when the mouse hovers over the target element.
10. An electronic device, characterized in that: The electronic device comprises: at least one processor; and, a memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the implementation method of optimizing the display of the front-end prompt component as described in any one of claims 1 to 8.
Citation Information
Patent Citations
Rapid analysis tool used for spreadsheet application
CN102982016A
Display method, apparatus, device and system used for numerical characters or character strings
CN106557452A
Prompt box display method, system and device and storage medium
CN112905925A
Text display method and device, electronic equipment, storage medium and product
CN114020224A
Chart display method and device, electronic equipment and storage medium
CN114065072A
